jas4 Posted August 23, 2007 Share Posted August 23, 2007 Hi, i've got a date coming into my webstite in the format: HH:MM:SS DD Mmm YY,YYYY PST (looks like 4:29:05 Aug 23, 2007 PDT) but I want it to convert it into a format for inserting into mysql either in DATE or TIMEDATE, i thought about using explode, but there explode takes everything seperated by a certain character, but how can it be done if there are 2 different characters? Quote Link to comment https://forums.phpfreaks.com/topic/66333-date-conversion/ Share on other sites More sharing options...
xyn Posted August 23, 2007 Share Posted August 23, 2007 function date2date($date) { $date = explode(" ", $date); #hh:mm:ss # dd # mmm# yy,yyyy # PDT return $date[3] . "-" . $date[2] . "-" . $date[1] . ", " . $date[0]; } to use. $string = "HH:MM:SS DD Mmm YY,YYYY PST"; date2date($string); Quote Link to comment https://forums.phpfreaks.com/topic/66333-date-conversion/#findComment-331841 Share on other sites More sharing options...
xyn Posted August 23, 2007 Share Posted August 23, 2007 The code above will look from: HH:MM:SS DD Mmm YY,YYYY PST to: YYYY-MM-DD, HH:MM:SS Quote Link to comment https://forums.phpfreaks.com/topic/66333-date-conversion/#findComment-331843 Share on other sites More sharing options...
jas4 Posted August 23, 2007 Author Share Posted August 23, 2007 ok that works pretty well, but for it to be perfect (for my needs ) I need to convert the month, i.e. Aug to a number i.e. 08. This is what is returned using the code above (thanks!) String 14:29:05 Aug 23, 2007 PDT new: 2007-23,-Aug, 4:29:05 Quote Link to comment https://forums.phpfreaks.com/topic/66333-date-conversion/#findComment-331966 Share on other sites More sharing options...
obsidian Posted August 23, 2007 Share Posted August 23, 2007 Try something like this: <?php function convertMyDate($date) { if (preg_match('|^([^\s]+)(.*)$|', $match)) { $time = $match[0]; $date = $match[1]; if (($ts = strtotime($date . $time)) !== FALSE) { return date('Y-d-m g:i:s', $ts); } else return FALSE; // invalid format } else return FALSE; // invalid format } ?> Hope that helps. Get familiar with the date/time conversion functions, and you can do almost anything you want. While this may not be exactly what you're after, it should definitely give you a push in the right direction. Quote Link to comment https://forums.phpfreaks.com/topic/66333-date-conversion/#findComment-331982 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.