Jump to content

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);

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.