Jump to content

Using strtotime to add to a time


HarryMW

Recommended Posts

Little confuzzled.

 

I have got a bunch of listings that will be printed at the end of the page but I need to subtract a time specified in a textbox (send it to itself) on the page.

 

$row['active'] = (strtotime('$row[etd]') + $rwy1 = $_POST['tmatttextfield']);

Well this just converts the time in the database which is always going to be a four character number such as: 1300 for 1PM into a time and adds the textbox value to the time.

 

<td width="50" bgcolor="<?php echo $bc?>"><strong><?php echo date('hi', $row['active']);?></strong></td>

That just prints it all out formatting the time to only Hours and Minutes.

 

So just to clarify the user would put in say '10' in the textbox which would constitute 10 minutes.

The code above should then define that '10' as 10 minutes.

Define the database result, ie. 1500 as 3PM and add the 10 minutes to it so it would become 1510.

 

I have been getting some wierd and wonderful results whilst trying to get it to work. Mainly just 1200 though.

 

Harry.

Link to comment
https://forums.phpfreaks.com/topic/232415-using-strtotime-to-add-to-a-time/
Share on other sites

I think if you just do something like this it could work:

 

$timetoadd = "10"; //from your text field
$time = "1500"; //from database

$newtime = date('Hi', strtotime($time." +".$timetoadd." minutes"));

 

I couldn't get it to work with something like 00:10:00 for some reason, but this is the concept. You can do +10 minutes, hours, months, years, etc.

 

Note: I used 'Hi', not 'hi' because lower-case h is 12-hour time, so you would get 0310.

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.