mikemeadeuk Posted August 23, 2012 Share Posted August 23, 2012 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 Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 23, 2012 Share Posted August 23, 2012 $date = date("Y/m/d", strtotime($date)); Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted August 23, 2012 Share Posted August 23, 2012 Homework? Quote Link to comment Share on other sites More sharing options...
Barand Posted August 23, 2012 Share Posted August 23, 2012 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)); Quote Link to comment Share on other sites More sharing options...
Jessica Posted August 23, 2012 Share Posted August 23, 2012 I always forget that it can't accept that format. Quote Link to comment Share on other sites More sharing options...
mikemeadeuk Posted August 23, 2012 Author Share Posted August 23, 2012 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 Quote Link to comment Share on other sites More sharing options...
mikemeadeuk Posted August 23, 2012 Author Share Posted August 23, 2012 just to add, the dates in my CSV are like this: 30/08/2012 00:00:00 I need them to be like this: 2012/08/30 00:00:00 Thanks Mike Really appreciate your time guys. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 23, 2012 Share Posted August 23, 2012 Strange, I could swear I'd given you the code to that. Do you read the responses to your question? Quote Link to comment Share on other sites More sharing options...
mikemeadeuk Posted August 23, 2012 Author Share Posted August 23, 2012 Hi Barand, Thanks for the reply, I tried this: {{list ($d, $m, $y) = explode('/', {item.StartDate}); $ymd = date ('Y-m-d', mktime(0,0,0,$m, $d, $y));}} And it didnt work. Am I missing something? thanks for your help. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 23, 2012 Share Posted August 23, 2012 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? Quote Link to comment Share on other sites More sharing options...
xyph Posted August 23, 2012 Share Posted August 23, 2012 Curly braces? Dot notation? Seems more like JavaScript Quote Link to comment Share on other sites More sharing options...
mikemeadeuk Posted August 23, 2012 Author Share Posted August 23, 2012 Hi, the curly braces are used to apply to php functions before my application imports it. Ahhh, I think the problem is because it is echoing //--> 2012-08-30 I need it to echo: //--> 2012-08-30 00:00:00 Quote Link to comment Share on other sites More sharing options...
mikemeadeuk Posted August 23, 2012 Author Share Posted August 23, 2012 ....not specifically 00:00:00, whatever the original time is. Quote Link to comment Share on other sites More sharing options...
Barand Posted August 23, 2012 Share Posted August 23, 2012 I need a way of converting DD/MM/YYYY to YYYY/MM/DD Amazing how goal posts can be moved Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted August 23, 2012 Share Posted August 23, 2012 I would go the str_replace route to get strtotime to work with that format - $dateStr = str_replace('/','-',$dateStr); Quote Link to comment Share on other sites More sharing options...
Barand Posted August 23, 2012 Share Posted August 23, 2012 or $dateStr = '30/08/2012 23:08:45'; list ($d, $m, $y, $h, $i, $s) = sscanf($dateStr, '%d/%d/%d %d:%d:%d'); $ymd = date('Y-m-d H-i:s', mktime($h,$i,$s,$m,$d,$y)) ; echo $ymd; //--> 2012-08-30 23-08:45 Quote Link to comment Share on other sites More sharing options...
mikemeadeuk Posted August 23, 2012 Author Share Posted August 23, 2012 Hi Barand, I did say this... the dates in my CSV are like this: 30/08/2012 00:00:00 I need them to be like this: 2012/08/30 00:00:00 Just didnt want to make a huuuuge title. sorry. Any ideas? Quote Link to comment Share on other sites More sharing options...
mikemeadeuk Posted August 23, 2012 Author Share Posted August 23, 2012 hi sorry, just seen your reply, ill try that... thanks Quote Link to comment Share on other sites More sharing options...
mikemeadeuk Posted August 23, 2012 Author Share Posted August 23, 2012 nope doesnt work. Does this work with eval()? Because this is what the software uses. thanks mike Quote Link to comment Share on other sites More sharing options...
scootstah Posted August 23, 2012 Share Posted August 23, 2012 nope doesnt work. Does this work with eval()? Because this is what the software uses. Oh god. Stop right there, you're doing something terribly wrong. Quote Link to comment Share on other sites More sharing options...
mikemeadeuk Posted August 23, 2012 Author Share Posted August 23, 2012 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 Quote Link to comment Share on other sites More sharing options...
mikemeadeuk Posted August 23, 2012 Author Share Posted August 23, 2012 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted August 23, 2012 Share Posted August 23, 2012 if you use PFM's suggestion you can do it as a one-liner $dateStr = '30/08/2012 23:08:45'; $ymd = date('Y-m-d H:i:s', strtotime(str_replace('/','-',$dateStr))) ; Quote Link to comment Share on other sites More sharing options...
Barand Posted August 24, 2012 Share Posted August 24, 2012 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 Quote Link to comment Share on other sites More sharing options...
Christian F. Posted August 24, 2012 Share Posted August 24, 2012 *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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.