1

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;
  }
3
  • 1
    What is the input? And have you tried strtotime()? Commented Dec 17, 2011 at 14:49
  • 1
    can you provide your sample input? Commented Dec 17, 2011 at 14:50
  • Show us the input $date string... Commented Dec 17, 2011 at 14:51

2 Answers 2

3

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.

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

6 Comments

Nice catch @Asaph, and your answer leaves his original code intact whereas I completely rewrote his function. +1.
I gave both an upvote, but I would prefer the strtotime() method, since it allows for more diversity of input (depending on usage). I'd also probably suggest removing the error suppressor on date().
I agree that strtotime() is a more elegant approach. There are 2 ways to approach questions like this. My approach answers the question "Why is my code not working?" and @Josh answers the question "What is the best way to do what I'm trying to do?". Both are valid and the OP is free to decide which one serves him better.
@Jared Farrish: I also agree about removing the @ operator. The only purpose it was serving was suppressing errors or warnings caused by the bug I identified in the code. Now that the bug is fixed, it's doing nothing. The error suppressor operator usually just masks problems and I personally avoid it. I left it in the answer because I just wanted to fix the bug and not make any distracting "enhancements".
I understand that, that wasn't a criticism per se. I don't know I would agree that suggesting removing it would be distracting or whatnot, but your description of why it was there and why it should be left out is part of the overall answer in my opinion.
|
1

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.

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.