Jump to content

Archived

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

zebe

PHP and time, how to subtract time

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!

Share this post


Link to post
Share on other sites
you may have to convert it to a unix stamp value and subtract 1800 seconds and convert it back.

Share this post


Link to post
Share on other sites
You can use strtotime():
[code]<?php
$tm = '13:56:32';
$ntim1 = date('G:i:s',strtotime($tm . ' -30 minutes'));
echo $ntim1;
?>[/code]

Ken

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
None of these techniques seem to work. When I have 14:00:00 initially, I get 14:30:00, not 13:30:00 as desired...

Any other ideas?

Thanks again for your help!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

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.