gamefreak13 Posted June 8, 2013 Share Posted June 8, 2013 I know I can use strtotime(), but I would feel safer specifying the format. How do I convert "6/8/2013 1:01:27 AM" into a unix time stamp while specifying "M/D/Y H:M:S (am/pm)" format? $date = strtotime('6/8/2013 1:01:27 AM'); echo $date; Quote Link to comment https://forums.phpfreaks.com/topic/278924-682013-10237-am-converted-to-unix-timestamp/ Share on other sites More sharing options...
cpd Posted June 8, 2013 Share Posted June 8, 2013 (edited) $date = "6/8/2013 1:01:27 AM"; $date = DateTime::createFromFormat("d/n/Y g:i:s A", $date); echo $date->format("FORMAT"); Edited June 8, 2013 by cpd Quote Link to comment https://forums.phpfreaks.com/topic/278924-682013-10237-am-converted-to-unix-timestamp/#findComment-1434822 Share on other sites More sharing options...
gamefreak13 Posted June 8, 2013 Author Share Posted June 8, 2013 Is there a way to do this prior to PHP 5.3? I have PHP 5.2.9 and DateTime is not available. Fatal error: Call to undefined method DateTime::createfromformat() Quote Link to comment https://forums.phpfreaks.com/topic/278924-682013-10237-am-converted-to-unix-timestamp/#findComment-1434905 Share on other sites More sharing options...
requinix Posted June 9, 2013 Share Posted June 9, 2013 If you're not on Windows, strptime. Quote Link to comment https://forums.phpfreaks.com/topic/278924-682013-10237-am-converted-to-unix-timestamp/#findComment-1434914 Share on other sites More sharing options...
kicken Posted June 9, 2013 Share Posted June 9, 2013 You could also manually parse it with a regex and use mktime Quote Link to comment https://forums.phpfreaks.com/topic/278924-682013-10237-am-converted-to-unix-timestamp/#findComment-1434919 Share on other sites More sharing options...
Barand Posted June 9, 2013 Share Posted June 9, 2013 or $d = '6/8/2013 1:01:27 PM'; list($m, $d, $y, $h, $i, $s, $a) = sscanf($d, '%d/%d/%d %d:%d:%d %s'); echo sprintf('%4d-%02d-%02d %02d:%02d:%02d', $y, $m, $d, $a=='AM'?$h:$h+12, $i, $s); //--> 2013-06-08 13:01:27 Quote Link to comment https://forums.phpfreaks.com/topic/278924-682013-10237-am-converted-to-unix-timestamp/#findComment-1434924 Share on other sites More sharing options...
Barand Posted June 9, 2013 Share Posted June 9, 2013 Don't store your dates as unix timestamps - store them as type DATETIME, format "yyyy-mm-dd hh:ii:ss" Quote Link to comment https://forums.phpfreaks.com/topic/278924-682013-10237-am-converted-to-unix-timestamp/#findComment-1434928 Share on other sites More sharing options...
Jessica Posted June 9, 2013 Share Posted June 9, 2013 Don't store your dates as unix timestamps - store them as type DATETIME, format "yyyy-mm-dd hh:ii:ss"Since we still don't have images on mobile view I will have to "like" this via a quote. *liked* Quote Link to comment https://forums.phpfreaks.com/topic/278924-682013-10237-am-converted-to-unix-timestamp/#findComment-1434981 Share on other sites More sharing options...
salathe Posted June 9, 2013 Share Posted June 9, 2013 I know I can use strtotime(), but I would feel safer specifying the format. Why would you feel safer? strtotime() has a finite list of formats that it will accept, yours is one of them. Quote Link to comment https://forums.phpfreaks.com/topic/278924-682013-10237-am-converted-to-unix-timestamp/#findComment-1434988 Share on other sites More sharing options...
gamefreak13 Posted June 10, 2013 Author Share Posted June 10, 2013 (edited) $d = '6/8/2013 1:01:27 PM'; list($m, $d, $y, $h, $i, $s, $a) = sscanf($d, '%d/%d/%d %d:%d:%d %s'); echo sprintf('%4d-%02d-%02d %02d:%02d:%02d', $y, $m, $d, $a=='AM'?$h:$h+12, $i, $s); //--> 2013-06-08 13:01:27 I tried this and it seems to work fine, thank you. Need I worry about varying lengths breaking this? If I'm understanding your code correctly, it searches for forward slashes and semi-colons to seperate the year, month, day, etc. I'll do some testing (like throwing 06/08/2013 at it instead of 6/8/2013), but thought I'd ask too. Edited June 10, 2013 by gamefreak13 Quote Link to comment https://forums.phpfreaks.com/topic/278924-682013-10237-am-converted-to-unix-timestamp/#findComment-1435110 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.