1

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.

asked Aug 17, 2009 at 12:05
3
  • What is the URI you requested? Commented Aug 17, 2009 at 12:08
  • 1
    action=update&shift=19&shift_name=Fun!&start_hours=4&start_minutes=0&start_ampm=PM&stop_hours=6&stop_minutes=0&stop_ampm=PM Commented 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 = '? Commented Aug 17, 2009 at 12:16

2 Answers 2

1

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']));
}
answered Aug 17, 2009 at 12:10
0

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.

answered Aug 17, 2009 at 12:31

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.