2

I'm trying to convert the string 11/24/2011 @ 01:15pm to a UNIX timestamp. The format is m-d-Y @ h:ia

I can't seem to get strtotime to work with the string. Is there a way to reverse the data function? Is my only choice to create a new non-default php function to convert the string?

The server is running CentOS 5, Apache 2.2 and PHP 5.2.17.

4
  • You're not trying to convert to "unix", that'd be something quite different. Fixed your terminology. Commented Nov 24, 2011 at 2:35
  • What do you mean by "non-default" function? What are the rest of the system details (OS &c.)? Commented Nov 24, 2011 at 3:07
  • It's a regular cpanel server. Centos 5, apache 2.2, etc Commented Nov 24, 2011 at 3:24
  • @user962449: relevant information should be edited into the question rather than being posted as comments. Visitors shouldn't have to read comments in order to understand questions or answers. You should also consider picking a meaningful username. One advantage to this is others can use at-replies and you'll get a notification that someone has addressed you in a comment. Commented Nov 24, 2011 at 3:41

5 Answers 5

5

Use the more modern DateTime class (so long as you're using >= 5.3).

$unix = DateTime::createFromFormat('m/d/Y @ h:ia', '11/24/2011 @ 01:15pm')
        ->getTimestamp();

CodePad.

Sign up to request clarification or add additional context in comments.

3 Comments

I get this error: Fatal error: Call to undefined method DateTime::createFromFormat()
@user962449: You may have an older PHP version (before 5.3). You could pre-process your string to suit strtotime().
Good answer - you should point out the required PHP version in the answer - not just the comments.
2

Under PHP 5.2, you can use strptime to parse a date-time string with a specific format, then use mktime to convert the result to a timestamp.

$timeString = '11/24/2011 @ 01:15pm';

$timeArray = strptime($timeString, '%m/%d/%Y @ %I:%M%p');
$timestamp = mktime(
    $timeArray['tm_hour'], $timeArray['tm_min'], $timeArray['tm_sec'], 
    $timeArray['tm_mon']+1, $timeArray['tm_mday'], $timeArray['tm_year']+1900
);

This should be abstracted as a function, possibly two:

function strptimestamp($date, $fmt) {
    $timeArray = strptime($date, $fmt);
    return mktime(
        $timeArray['tm_hour'], $timeArray['tm_min'], $timeArray['tm_sec'], 
        $timeArray['tm_mon']+1, $timeArray['tm_mday'], $timeArray['tm_year']+1900
    );
}

function strpmy($date) {
    return strptimestamp($date, '%m/%d/%Y @ %I:%M%p');
}

Support for parsing the period abbreviation appears to vary from OS to OS. If the above doesn't work on a particular OS, try "%P" instead of "%p" or pass the time string through strtoupper (or both). The following should work under any OS, though it's preferable to get strptime to handle the entirety of the parsing, as the following is less suitable as the basis for a generic strptimestamp function.

static $pm_abbrevs = array('pm' => 1, 'p.m.' => 1, 'µµ' => 1, 'µ.µ.' => 1);
$timeString = '11/24/2011 @ 01:15pm';

$timeArray = strptime($timeString, '%m/%d/%Y @ %I:%M');
$period = strtolower($timeArray['unparsed']);
if (isset($pm_abbrevs[$period])) {
    $timeArray['tm_hour'] += 12; 
}
$timestamp = mktime(
    $timeArray['tm_hour'], $timeArray['tm_min'], $timeArray['tm_sec'], 
    $timeArray['tm_mon']+1, $timeArray['tm_mday'], $timeArray['tm_year']+1900
);

4 Comments

I liked this answer too, except I couldn't get it to work. It could very well be due to the fact that "Note: Internally, this function calls the strptime() function provided by the system's C library. This function can exhibit noticeably different behaviour across different operating systems."
"Couldn't get it to work" says very little. How, exactly, did it not work?
Very odd, but I had to change the %P to %p to get this example to work. Note that this is quite contrary to what the docs say.
It looks like CentOS's strptime doesn't support '%P', but does have a '%p'. According to PHP's doc, OS X also doesn't support '%P', though this wasn't what I observed. Furthermore, '%p' worked on OS X to parse the lowercase 'pm'.
0

If you replace the ' @ ' with a space, then strtotime should be able to understand it natively.

<?php
$x = "11/24/2011 @ 01:15pm";
$x = str_replace(" @ ", " ", $x);
$y = strtotime($x);
$z = date("m-d-Y @ h:ia", $y);
echo "x: $x<br />\n";
echo "y: $y<br />\n";
echo "z: $z<br />\n";
?>

Output:

x: 11/24/2011 01:15pm
y: 1322140500
z: 11-24-2011 @ 01:15pm

3 Comments

didn't work either. $y didn't have any value. $x and $z were fine. Sorry, $z had 12-31-1969 @ 04:00pm since $y had no value.
I ran it on my web server before posting. I'll check my PHP version.
I'm running the same version - 5.2.17.
0

Case sensitivity may be your issue http://php.net/manual/en/datetime.formats.php. Perhaps run $x through strtoupper() first then str_replace('@', '', $x) (notice it's replacing @ with an empty string), then try strtotime(). Hope this helps.

Comments

0
$search = array('/',',','@');
$replace = array('-','','');
echo strtotime( str_replace($search,$replace,'16/7/2013 @ 7:30AM') );

this will replace the parts of the string in the time string you are trying to convert into a format that is acceptable to strtotime. You can always add more string parts you want to replace to the arrays.

Also you dont need to have latest php for this.

Output:

1373952600

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.