Reputation: 309
Problem with Date function
output: 00:00am on Sunday December 17th, 2011
Problem hour and minutes always display 00:00 and sometime Year is also incorrect
Input date('Y-m-d H:i:s', time());
functions
public function perfect_date_format($date) {
$dated = str_replace(array(" ", ":"), "-", $date);
list($year, $month, $day, $hour, $minute) = explode("-", $date);
$niceday = @date("H:ia \o\\n\ l, F jS, Y", mktime($hour, $minute, 0, $month, $day, $year));
return $niceday;
}
Upvotes: 1
Views: 268
Reputation: 162771
You have a typo. The 2nd argument to explode()
should be $dated
, rather than $date
. If you do this, the time is correctly displayed.
<?php
function perfect_date_format($date) {
$dated = str_replace(array(" ", ":"), "-", $date);
list($year, $month, $day, $hour, $minute) = explode("-", $dated);
$niceday = @date("H:ia \o\\n\ l, F jS, Y", mktime($hour, $minute, 0, $month, $day, $year));
return $niceday;
}
echo perfect_date_format('2011-12-17 03:45:00') . "\n";
?>
This outputs:
03:45am on Saturday, December 17th, 2011
BTW: It's safe to remove the error suppressor operator (ie. @
) in the code above. It was simply suppressing errors or warnings caused by the bug identified above. Now that the bug is fixed, the @
isn't doing anything.
Upvotes: 3
Reputation: 11070
It's very difficult to know hot to correct your code without knowing what you're passing to perfect_date_format
, but I'll make a wild guess and assume that strtotime
understands how to parse your $date
:
public function perfect_date_format($date) {
$time = strtotime($date);
return date("H:ia \o\\n\ l, F jS, Y", $time);
}
If that doesn't work, then please provide the format of the data you're passing as $date
.
Upvotes: 1