Petsmacker Posted November 21, 2006 Share Posted November 21, 2006 I was hoping to turn this:[i]Nov 13, 2006, 10:06pm[/i]into a UNIX timestamp when posted in a formBearing in mind its not 24 hours time and the times can not be changed. I'm basically transferring posts from a proboards board to my own site but that means transferring all 19,000 posts so obviously we're looking for ways to speed up the process of the transfer. Hope you can help. Quote Link to comment https://forums.phpfreaks.com/topic/27930-convert-to-timestamp/ Share on other sites More sharing options...
hitman6003 Posted November 21, 2006 Share Posted November 21, 2006 have you tried strtotime?http://www.php.net/strtotimeif it won't work "out-of-the-box", you may just have to rearrange the parts a little:[code]function convert($date) { //format: "Nov 13, 2006, 10:06pm"; //eliminate commas and seperate the parts $date = explode(" ", str_replace(",", "", $date)); //determine am / pm, remove the text, adjust the hours if necessary if (substr($date['3'], -2, 2) == "pm") { list($hour, $minute) = explode(":", rtrim($date[3], 'pm'); $hour += 12; } else { list($hour, $minute) = explode(":", rtrim($date[3], 'am'); } //reconstruct and pass to strtotime return strtotime($date[2] . "-" . $date[0] . "-" . $date[1] . " " . $hour . ":" . $minute);}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/27930-convert-to-timestamp/#findComment-127741 Share on other sites More sharing options...
Petsmacker Posted November 21, 2006 Author Share Posted November 21, 2006 I know there's a way of converting a timestamp to time with gmdate:e.g - $timeworkedout=gmdate("d M Y H:i:s", $time);No way to convert it back?Is there a way to have it come out as a variable? I mean I could sort it so that if the 'am' or 'pm' bit is a problem I could have it convert the hour to 24 hour format by having the PHP recognise the letters 'am' or 'pm'. Quote Link to comment https://forums.phpfreaks.com/topic/27930-convert-to-timestamp/#findComment-127746 Share on other sites More sharing options...
hitman6003 Posted November 21, 2006 Share Posted November 21, 2006 [quote author=Petsmacker link=topic=115710.msg471205#msg471205 date=1164077236]No way to convert it back?[/quote]See my above post. Quote Link to comment https://forums.phpfreaks.com/topic/27930-convert-to-timestamp/#findComment-127748 Share on other sites More sharing options...
Petsmacker Posted November 21, 2006 Author Share Posted November 21, 2006 My code is:<?$dates="Jun 2, 2006, 9:13pm";function convert($date) { //format: "Nov 13, 2006, 10:06pm"; //eliminate commas and seperate the parts $date = explode(" ", str_replace(",", "", $dates)); //determine am / pm, remove the text, adjust the hours if necessary if (substr($date['3'], -2, 2) == "pm") { list($hour, $minute) = explode(":", rtrim($date[3], 'pm')); $hour += 12; } else { list($hour, $minute) = explode(":", rtrim($date[3], 'am')); } //reconstruct and pass to strtotime return strtotime($date[2] . "-" . $date[0] . "-" . $date[1] . " " . $hour . ":" . $minute);}?>But get no output whatsoever. Quote Link to comment https://forums.phpfreaks.com/topic/27930-convert-to-timestamp/#findComment-127757 Share on other sites More sharing options...
hitman6003 Posted November 21, 2006 Share Posted November 21, 2006 You aren't echoing out anything:[code]echo convert("Jun 2, 2006, 9:13pm");[/code] Quote Link to comment https://forums.phpfreaks.com/topic/27930-convert-to-timestamp/#findComment-127760 Share on other sites More sharing options...
Petsmacker Posted November 21, 2006 Author Share Posted November 21, 2006 Yaharharhar!!The ouput I get is '-1'. Quote Link to comment https://forums.phpfreaks.com/topic/27930-convert-to-timestamp/#findComment-127761 Share on other sites More sharing options...
kenrbnsn Posted November 21, 2006 Share Posted November 21, 2006 Why make it complicated?[code]<?php$str = 'Nov 13, 2006, 10:06pm';echo strtotime(str_replace(',','',$str));?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/27930-convert-to-timestamp/#findComment-127768 Share on other sites More sharing options...
Petsmacker Posted November 21, 2006 Author Share Posted November 21, 2006 It works, thanks for both your helps. Quote Link to comment https://forums.phpfreaks.com/topic/27930-convert-to-timestamp/#findComment-127880 Share on other sites More sharing options...
hitman6003 Posted November 22, 2006 Share Posted November 22, 2006 [quote author=kenrbnsn link=topic=115710.msg471228#msg471228 date=1164079733]Why make it complicated?[/quote]I agree...that's why my first suggestion was to use strtotime without modifying the string and see if it works...apparently that got overlooked......... Quote Link to comment https://forums.phpfreaks.com/topic/27930-convert-to-timestamp/#findComment-128247 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.