Jump to content


Photo

PHP and time, how to subtract time


  • Please log in to reply
6 replies to this topic

#1 zebe

zebe
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 27 July 2006 - 03:34 PM

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!

#2 otuatail

otuatail
  • Members
  • PipPipPip
  • Advanced Member
  • 960 posts

Posted 27 July 2006 - 03:42 PM

you may have to convert it to a unix stamp value and subtract 1800 seconds and convert it back.


#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 27 July 2006 - 03:44 PM

You can use strtotime():
<?php
$tm = '13:56:32';
$ntim1 = date('G:i:s',strtotime($tm . ' -30 minutes'));
echo $ntim1;
?>

Ken


#4 Gast

Gast
  • Members
  • PipPipPip
  • Advanced Member
  • 131 posts
  • LocationSouth East England

Posted 27 July 2006 - 03:44 PM

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

Try that.
OS and Internet Specs:
Windows XP (with SP2), 1680x1050, PHP 4, MySQL 3, Internet Explorer 6 &7 /FireFox 2.0/Opera 9.2

Tutorials:
Simple Introduction to AJAX and XMLHttpRequest
Creating Live Data with AJAX Making a Google Suggest like application

#5 zebe

zebe
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 27 July 2006 - 05:16 PM

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!

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 27 July 2006 - 05:24 PM

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

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

Ken

#7 zebe

zebe
  • Members
  • PipPip
  • Member
  • 24 posts

Posted 27 July 2006 - 05:33 PM

Perfect! Thank you so much.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users