views:

89

answers:

2

My below function that is copied from another function that works fine. Should get values from the query string and turn them into a date:

function updateShift()
    {
     echo $name = $_GET['shift_name'];

     echo $start_date = date('Y-m-d H:i:s', strtotime("{$_GET['start_hours']}:{$_GET['start_minutes']} {$_GET['start_ampm']}"));
     echo $stop_date = date('Y-m-d H:i:s', strtotime("{$_GET['stop_hours']}:{$_GET['stop_minutes']} {$_GET['stop_ampm']}"));
}

However it returns:

Shift Name
1969-12-31 17:00:00
1969-12-31 17:00:00

Any idea why this works fine elsewhere but not here? The query string is there as evidenced by the shift_name coming through correctly.

+1  A: 

What if you do this:

function updateShift()
    {
        echo $name = $_GET['shift_name'];

        echo $start_date = date('Y-m-d H:i:s', strtotime($_GET['start_hours'].':'.$_GET['start_minutes'].' '.$_GET['start_ampm']));
        echo $stop_date = date('Y-m-d H:i:s', strtotime($_GET['stop_hours'].':'.$_GET['stop_minutes'].' '.$_GET['stop_ampm']));
}

or

function updateShift()
    {
        echo $_GET['shift_name'];

        echo date('Y-m-d H:i:s', strtotime($_GET['start_hours'].':'.$_GET['start_minutes'].' '.$_GET['start_ampm']));
        echo date('Y-m-d H:i:s', strtotime($_GET['stop_hours'].':'.$_GET['stop_minutes'].' '.$_GET['stop_ampm']));
}
Time Machine
A: 

The dates you received were the start of the Unix epoch since your date function call was getting false (or 0) as the second argument. I just ran some quick tests using your code and I am seeing that strtotime is returning false with the values supplied.

echo "{$_GET['start_hours']}:{$_GET['start_minutes']} {$_GET['start_ampm']}";
=> '4:0 PM'

You need to make sure you have 2 digits in your minutes field to allow strtotime to see it as a valid time and to parse it correctly. To to this you can either update your query string, use str_pad or sprintf to ensure you have the 2 digits required for the time to be valid.

Peer Allan