Reply to Re: subtract time from a date

Your name:

Reply:


Posted by Erwin Moller on 03/29/07 08:09

Arne Gemsa wrote:

> Hi,
>
> I want to get a starttime. To get this I have to subtract the runtime from
> a unit e.g. 08:40:15 from a date e.g. 2007-03-29 16:48:30. So the
> starttime is 2007-03-29 08:08:15.
>
> Is there any function in php to do so, or must I do it by myself?

Hi,

Easiest way I know of (that also works in almost any language I have seen)
is simply cast your startdate to a UTS (Unix Time Stamp): The number of
seconds that passed since 1970.
--> Look at function strtotime():
$EndtimeUTS = strtotime("2007-03-29 16:48:30");

Then calculate the number of seconds in your interval.
I don't know of a function (might very well exists), but you could simply do
something like this:
$interval = "08:40:15";
list($hours,$minutes,$secs) = explode(":",$interval);
$secsInInterval = $secs + $minutes*60 + $hours*60*60;

Subtract the latter from the first.

$starttimeUTS = $EndtimeUTS-$secsInInterval;

Cast the resulting UTS to a date.
-->lookup function date()

$formattedStartTime = date("Y-m-d H:i:s",$starttimeUTS);

Not tested, could contain typos and bugs. :P

>
> thx

Regards,
Erwin Moller

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация