Jump to content

Convert to timestamp?


Petsmacker

Recommended Posts

I was hoping to turn this:
[i]Nov 13, 2006, 10:06pm[/i]
into a UNIX timestamp when posted in a form

Bearing 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.
Link to comment
Share on other sites

have you tried strtotime?

http://www.php.net/strtotime

if 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]
Link to comment
Share on other sites

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'.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.