• Resolved chemistrap

    (@chemistrap1)


    Hello again,
    I’m importing products which type is books. And there has some custom specification like published date. But this date like 2018/04 in my xml file. But i want to convert this to April 2018.

    How can i do that in my local date (tr_TR) ?
    Thank you

    • This topic was modified 6 years, 7 months ago by chemistrap.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Here is one you can use. This will output ‘April 2018’

    You will need to add this to your wp all import function editor.

    function formatDate($str) {
      $yr = preg_replace('#\/[^/]*$#', '', $str);
      $month = preg_replace('/^.*\/\s*/', '', $str);
      $mth = 'Invalid Month';
      
      if($month >= 1 && $month <= 12){
        $mth = date("F", strtotime("2001-" . $month . "-01"));
      }
      return $mth.' '.$yr;
    }

    Call it as such.
    [formatDate({yourXmlDate[1]})]

    Plugin Author WP All Import

    (@wpallimport)

    Hi @chemistrap1

    You can do this with a custom PHP function: https://www.wpallimport.com/documentation/developers/execute-php/ (as mentioned by @duceduc above).

    It looks like this snippet above should work, and here’s another example snippet in case it helps:

    function changeDate( $date ) {
    	if ( $dateObj = DateTime::createFromFormat( "Y/m", $date ) ) {
    		return $dateObj->format( "F Y" );
    	} else {
    		return $date;
    	}
    }
    • This reply was modified 6 years, 6 months ago by WP All Import.
    Thread Starter chemistrap

    (@chemistrap1)

    Thank you for help. I guess your snipper code more logical other but thanks anyway to guys.

    Thread Starter chemistrap

    (@chemistrap1)

    @duceduc your code was work but only translate problem occured. How can i handle that?
    Thank you.

    Thread Starter chemistrap

    (@chemistrap1)

    @duceduc thank you so much again.
    I handle localization with i18n

    if change your code like that

    function formatDate($str) {
      $yr = preg_replace('#\/[^/]*$#', '', $str);
      $month = preg_replace('/^.*\/\s*/', '', $str);
      $mth = 'Invalid Month';
      
      if($month >= 1 && $month <= 12){
        $mth = date_i18n("F", strtotime("2001-" . $month . "-01"));
      }
      return $mth.' '.$yr;
    }

    it worked like what exactly want.

    Plugin Author WP All Import

    (@wpallimport)

    I’m marking this as resolved, but feel free to continue the custom code discussion in this thread if you’d like.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Date Convert to Text’ is closed to new replies.