PHP Adding 15 minutes to Time value

后端 未结 7 2275
一整个雨季
一整个雨季 2020-11-27 07:35

I have a form that receives a time value:

$selectedTime = $_REQUEST[\'time\'];

The time is in this format - 9:15:00 - which is 9:15am. I t

7条回答
  •  孤独总比滥情好
    2020-11-27 07:40

    Though you can do this through PHP's time functions, let me introduce you to PHP's DateTime class, which along with it's related classes, really should be in any PHP developer's toolkit.

    // note this will set to today's current date since you are not specifying it in your passed parameter. This probably doesn't matter if you are just going to add time to it.
    $datetime = DateTime::createFromFormat('g:i:s', $selectedTime);
    $datetime->modify('+15 minutes');
    echo $datetime->format('g:i:s');
    

    Note that if what you are looking to do is basically provide a 12 or 24 hours clock functionality to which you can add/subtract time and don't actually care about the date, so you want to eliminate possible problems around daylights saving times changes an such I would recommend one of the following formats:

    !g:i:s 12-hour format without leading zeroes on hour

    !G:i:s 12-hour format with leading zeroes

    Note the ! item in format. This would set date component to first day in Linux epoch (1-1-1970)

提交回复
热议问题