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; 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 $date = "6/8/2013 1:01:27 AM"; $date = DateTime::createFromFormat("d/n/Y g:i:s A", $date); echo $date->format("FORMAT"); 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() 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. 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 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 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" 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* 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. 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 $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. 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
Archived
This topic is now archived and is closed to further replies.