I am having trouble echoing out the correct unix timestamp for the date saved in the database.
$activeWorkRow['WorkDate'] = "9/24/2015 1:45:53 PM";
$locateDate = $activeWorkRow['WorkDate'];
$locateDate = str_replace('/', '-', $locateDate);
//$locateDate = date('m-d-Y', strtotime($locateDate));
//$locateDate = strtotime(date($locateDate));
echo $locateDate."<br>";
Output:
9-24-2015 1:45:53 PM
Next:
$locateDate = $activeWorkRow['WorkDate'];
$locateDate = str_replace('/', '-', $locateDate);
$locateDate = date('m-d-Y', strtotime(locateDate));
//$locateDate = strtotime(date($locateDate));
Output:
12-31-1969
I am trying to get to the unix timestamp so I can compare it with others.
Your issue is the str_replace('/', '-', $locateDate);
. Your date is given as m/d/y
and with that replacement you're converting it to m-d-y
.
But strtotime
treats -
as indicator for the European d-m-y
format. And in that format, 9/24/2015
is not a valid date, leading to your observed output (which is equal to date('m-d-Y', 0)
).
Simply skip the str_replace
:
$activeWorkRow['WorkDate'] = "9/24/2015 1:45:53 PM";
$locateDate = $activeWorkRow['WorkDate'];
echo strtotime($locateDate);
Output:
1443116753
Cf. the manual for more details and caveats:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. If, however, the year is given in a two digit format and the separator is a dash (-, the date string is parsed as y-m-d.
Note that when displaying those timestamps you might also want to think about timezones (strtotime
uses the default unless specified otherwise), but as long as you're only comparing timestamps generated by the same function you should be fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With