Jump to content

Convert DD/MM/YYYY to YYYY/MM/DD ?


mikemeadeuk

Recommended Posts

Hi Guys,

 

I need a way of converting DD/MM/YYYY to YYYY/MM/DD via one php line. any ideas how I would accomplish this?

 

Basically, I have a bit of kit that allows me to change CSV data before it imports into my application. However I seem to only be able to use standard PHP functions and not custom functions.

 

Any ideas would be much appreciated.

 

cheers,

 

mike

 

 

Link to comment
https://forums.phpfreaks.com/topic/267498-convert-ddmmyyyy-to-yyyymmdd/
Share on other sites

Unfortunately dd/mm/yyyy is not recognised by strtotime (http://forums.phpfreaks.com/index.php?topic=364145.msg1724276#msg1724276).

 

list ($d, $m, $y) = explode('/', $dateStr);
$ymd = date ('Y-m-d', mktime(0,0,0,$m, $d, $y));

Hi Guys,

 

thanks for the replys. I am limited to what I can do, as I can only enter the php code into a text box. This is the instructions I have:

 

------

Advanced expressions use php eval() function, it can be also very powerful, example:

{{substr("{item.expiry_date}",0,4}}

------

 

 

Do you know if there is a way to convert the date into the format I need using eval()? If you wondering, {item.expiry_date} refers to the column expiry_date in my CSV file.

 

Please excuse my limited knowledge.

 

thanks

 

mike

 

 

This is my code applied to your date

 

$dateStr = '30/08/2012 00:00:00';

list ($d, $m, $y) = explode('/', $dateStr);
$ymd = date ('Y-m-d', mktime(0,0,0,$m, $d, $y));

echo $ymd;         //--> 2012-08-30 

 

In which way did it not work?

hi, it works fine in in a standalone php file yes. However I am using magento with this:  http://sourceforge.net/apps/mediawiki/magmi/index.php?title=Value_Replacer

 

And this application uses eval() to allow php functions to alter the CSV file before importing to my magento website.

 

This is what I did, using Barands code, and I placed it within double curly braces as the application advises. And {item.StartDate} refers to StartDate in my CSV file.

 

{{list ($d, $m, $y, $h, $i, $s) = sscanf({item.StartDate}, '%d/%d/%d %d:%d:%d');$ymd = date('Y-m-d H-i:s', mktime($h,$i,$s,$m,$d,$y)) ;}}

 

Any ideas what Im doing wrong?

 

Thanks

 

Mike

sorry, this even:

 

{{list ($d, $m, $y, $h, $i, $s) = sscanf({item.StartDate}, '%d/%d/%d %d:%d:%d');{item.StartDate} = date('Y-m-d H-i:s', mktime($h,$i,$s,$m,$d,$y)) ;}}

 

This works perfectly well in a single php file, but if I put it within eval() it highlights as an error, I'm not overly clued up as you can tell, so hopefully someone can point me in the right direction.

 

thanks

I know I'll probably get banned from this site for using the e function

 

$dateStr = '30/08/2012 23:08:45';

echo eval("return date('Y-m-d H:i:s', strtotime(str_replace('/','-',\$dateStr)));");  //-> 2012-08-30 23:08:45

*Readies the tar, feathers, torches and pitchforks.* Bad, Barand, bad! Now you'll have to be punished.

 

mikemeadeuk: I'd advice you to edit the actual code in question, instead of trying to write pseudo-code for a third party wrapper with (apparently) lacking documentation. Reduce the complexity when it is not needed, and you'll find that a whole lot of headaches will simply vanish.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.