简体   繁体   中英

How can I get the AM/PM in this PHP code to work correctly?

I have a form on the back end of a site to create events. The user selects a date, time, and AM or PM for the event. This is working correctly however once the user submits the data the PM is changing to AM on the front in certain instances that involve 12:00. I believe it has something to do with the date...reason being that if I try to create an event that starts at 12:00 PM and ends at 9:00 PM it will automatically change the 12:00 PM to AM.

If I try to create an event with a start time of 12:00 AM and end time of 9:00 AM it changes the 12:00 AM to PM. Is something off with the date on the server?

In the database the time is stored as follows for 12:00 AM 00:00:00 and 12:00:00 for PM which is correct.

Here's the code:

// Add a new event.
if (isset($_POST['add-event-submit']))
{
    $upload_ext    =  pathinfo('../globals/uploads/events/'.basename($_FILES['event-image']['name']),PATHINFO_EXTENSION);
    $upload_name   =  'image-'.date('mdyHis').uniqid().'.'.$upload_ext;
    $upload_path   =  '../globals/uploads/events/'.$upload_name;

    $event_name         =  iconv('UTF-8', 'ASCII//TRANSLIT', $_POST['event-name']);
    $event_type         =  iconv('UTF-8', 'ASCII//TRANSLIT', $_POST['event-type']);
    $event_description  =  iconv('UTF-8', 'ASCII//TRANSLIT', $_POST['event-description']);
    $event_image        =  $upload_name;

    $event_start_date   =  date('Y-m-d', strtotime($_POST['event-start-date']));
    $event_end_date     =  date('Y-m-d', strtotime($_POST['event-end-date']));

    $event_start_ampm     =  $_POST['event-start-ampm'];
    if ($event_start_ampm == 'am') { $event_start_time = date('h:i:s', strtotime($_POST['event-start-time'])); }
    if ($event_start_ampm == 'pm') { $event_start_time = date('H:i:s', strtotime($_POST['event-start-time']) + 43200); }
    $event_full_start     = $event_start_date.' '.$event_start_time;

    $event_end_ampm       =  $_POST['event-end-ampm'];
    if ($event_end_ampm   == 'am') { $event_end_time   = date('h:i:s', strtotime($_POST['event-end-time'])); }
    if ($event_end_ampm   == 'pm') { $event_end_time   = date('H:i:s', strtotime($_POST['event-end-time']) + 43200); }
    $event_full_end       = $event_end_date.' '.$event_end_time;

Should I just use something like this (even though this has to be a workaround):

$time = $_POST['event-start-time'];
$am = 'am';
$pm = 'pm';

$time_code = $time . $am; //add am or pm depending on user selection
$event_start_time   = date('h:i a', strtotime($time_code));

So it turns out that I was looking in the wrong spot! I just updated the value for 12:00 in the drop down menu. It was set to 12:00 instead of 00:00 which is what was throwing off the calculation when adding 12 hours (+4300). It's working correctly now!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM