469,935 Members | 2,225 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,935 developers. It's quick & easy.

subtract time from a date

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?

thx
Mar 29 '07 #1
3 24959
Arne Gemsa wrote:
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?
http://php.net/strtotime will convert it to a timestamp, then you can
subtract the appropriate amount of time (hint - in seconds) and then
reformat using http://php.net/date

Or you can use http://php.net/mktime after working out the current year,
month etc with http://php.net/date and then adding/subtracting the
appropriate hours and minutes etc.

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
Mar 29 '07 #2
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
Mar 29 '07 #3
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?
Basically, convert the end time and duration into unix timestamps if
they're not already, and just do the subtraction.

With the end time and duration as a string:

$startTime = strtotime("2007-03-29 16:48:30") - strtotime("08:40:15");

With end time as a string and duration as numbers:

$duration = $durHours * 3600 + $durMinutes * 60 + $durSeconds;
$startTime = strtotime("2007-03-29 16:48:30") - $duration;

With end time as a string and duration as a timestamp:

$startTime = strtotime("2007-03-29 16:48:30") - $duration;

If end time is already a timestamp:

$startTime = $endTime - strtotime("08:40:15");

or:

$duration = $durHours * 3600 + $durMinutes * 60 + $durSeconds;
$startTime = $endTime - $duration;

or:

$startTime = $endTime - $duration;

Then, if you want the answer as a string:

$startTimeStr = strftime($startTime);

Denis McMahon
Apr 1 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Russ Green | last post: by
5 posts views Thread by Sandy | last post: by
4 posts views Thread by meltedown | last post: by
2 posts views Thread by barronmo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.