Jump to content

PHP and time, how to subtract time


zebe
 Share

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

This thread is more than a year old.

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.

 Share

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