Reputation: 3873
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.
Upvotes: 2
Views: 5373
Reputation: 11
$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
Upvotes: 0
Reputation: 77400
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
);
Upvotes: 2
Reputation: 490143
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();
Upvotes: 5
Reputation: 519
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.
Upvotes: 0
Reputation: 5386
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
Upvotes: 0