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.
-
What is the URI you requested?user142019– user1420192009年08月17日 12:08:00 +00:00Commented Aug 17, 2009 at 12:08
-
1action=update&shift=19&shift_name=Fun!&start_hours=4&start_minutes=0&start_ampm=PM&stop_hours=6&stop_minutes=0&stop_ampm=PMian– ian2009年08月17日 12:14:31 +00:00Commented Aug 17, 2009 at 12:14
-
Are you sure updateShift is executed, and not another function? I sometimes have this problem. Also, what if you remove '$name = ', '$start_date = ', and '$stop_date = '?user142019– user1420192009年08月17日 12:16:59 +00:00Commented Aug 17, 2009 at 12:16
2 Answers 2
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']));
}
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.