Jump to content

Adding minutes to time


BelowZero

Recommended Posts

I'm trying to add some minutes ($interval) to any given time, but my results aren't as expected...

 

<?php
$year="2012";
$opentime="09";
$interval="10";
$thedate = "$year:01:01 $opentime:00:00";
$startdate = strtotime($thedate);

$date = date("Y-m-d",$startdate);
$time = date("g:i a",$startdate);

$newtime = strtotime("+$interval", $time);
$nexttime = date("g:i a",$newtime);

echo $date;
echo "<br>";
echo $time;
echo "<br>";
echo $nexttime;

is returning:

2012-01-01

9:00 am

7:10 pm

 

I was expecting the last to be 9:10 am.

Can someone point me in the right direction? Thanks.

Link to comment
https://forums.phpfreaks.com/topic/259139-adding-minutes-to-time/
Share on other sites

TIME, as computers handle it, is not in the format of 09:15.  TIME (like the second argument to date() and strtotime()) is the number of seconds since 01/01/1970.  You are trying to give "7:10" when 138348402382 is expected.

 

You have to do this math by hand, there is no way to add minutes and hours in PHP without also giving it a date string.

 

Also, you're using "+10" as your argument to strtotime.  10 what?  Strtotime assumes seconds, since as I said above time is measured in seconds.

Thanks ManiacDan.

 

Here's the code that works in case anyone is interested.

<?php

$year= "2012";
$opentime= "09";
$interval= "10";

$thedate = "$year:01:01 $opentime:00:00";
$startdate = strtotime($thedate);

$date = date("Y-m-d",$startdate);
$time = date("g:i a",$startdate);

$newtime = strtotime("+$interval minute", $startdate);

$nexttime = date("g:i a",$newtime);

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.