Jump to content

PHP and time, how to subtract time


zebe

Recommended Posts

Hi,

I have a variable that is storing a time value that has been retrieved from a database. It is in the format of: HH:MM:SS. What i need to do is subtract a half hour from this value, does PHP have any way of doing this?

I was thinking about using explode to get the hours and seconds and then using conditionals to determine how to reformatt it, but that seems bulky to me... Just wondering if anyone has a more efficient solution. Thanks for the help!
Link to comment
https://forums.phpfreaks.com/topic/15810-php-and-time-how-to-subtract-time/
Share on other sites

[code]<?php
$mysqldate = date("H:i:s", strtotime($row['your_mysql_date']));
$newdate = date("H:", strtotime($row['your_mysql_date'])) . (date("i:", strtotime($row['your_mysql_date'])) -30) . date("s", strtotime($row['your_mysql_date']));

echo $newdate;
?>[/code]

Try that.
My (modified) code works fine:
[code]<?php
$tm = '14:00:00';
$ntim1 = date('G:i:s',strtotime($tm . ' -30 minutes'));
echo $ntim1;
?>[/code]

You may not have noticed that I took out the space I originally had after the '-' in the original post.

Ken

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.