473,611 Members | 2,236 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 25189
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,$mi nutes,$secs) = explode(":",$in terval);
$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()

$formattedStart Time = date("Y-m-d H:i:s",$startti meUTS);

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:4 0: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:4 0: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($start Time);

Denis McMahon
Apr 1 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1912
by: Russ Green | last post by:
I'm trying to perform several manipulations to time values (all will be stored in a access database). I need to find the elapsed time between a start time and a finish time and I also need to be able to find the SUM of multiple time values. I know how to do this first part of the using the following code but how do I add multiple time values together when they are in the "15:53:12" format? ____START CODE_____ Dim TimeStart, TimeStop...
8
25378
by: dlx_son | last post by:
Here is the code so far <form name="thisform"> <h3>Enter time to add to or subtract from:</h3> (If not entered, current time will be used)<br> Day: <input name="d1" alt="Day of month" size=3> Month: <input name="m1" alt="Month" size=3> Year: <input name="y1" alt="Year" size=5> (4 digits for year, e.g.
5
2745
by: Sandy | last post by:
Hello I need to subtract a pm time from an am time, e.g. 10:00 a.m. from 1:00 p.m. to get 3 I have two textboxes for the time values and next to each is a dropdownlist to click indicating a.m. or p.m Any help will be greatly appreciated Sandy
5
6511
by: cvisal | last post by:
Hi all Im working on productivity calculations (Time calculations) and need some help in coding. Database Tool:MS-Access 2003. The general operator punch-in time is 5:30 AM and the punch-out time is 2:00PM. The Break times are 1) 9:30 AM to 9:45 AM 2) 11:00AM to 11:30 AM 3) 12:30PM to 12:45 PM
4
16705
by: meltedown | last post by:
I can't see what I'm doing wrong. I'm subtracting 60*60*24 from a unix time stamp and the result is 23 hours earlier, not 24. Start with a unix time stamp: $unixtime=1144018006; convert it to a date: $date=getdate($unixtime); date Array (
8
12820
by: Remington | last post by:
I am using windows 2000pro with access 2000. I am trying to make a database for our HR department, that would allow our HR Director to type in an employee's ID number into a form and then select the dates the employee took off from work. (I have the calander add-in 8.0 setup to easily select the dates already) There are two Date Fields, "Leave Date", and "Return Date" where the calander selections are stored. They are in the 12/25/2006...
2
19814
by: jld730 | last post by:
Hello All, I am very new to Python and Oracle, and I have a question for you all. How do you add/subtract minutes to a date? Is this possible? I need to add/subtract 30 minutes (or 60, or 90, etc) to/from a user-defined date-and-time, and then select based on that date-and-time-range. So, if I have a date looking like '2002-03-14 17:42:00', 'YYYY-MM-DD HH24:MI:SS', is there a way to add 30 minutes to it to get the right-side of the time...
6
4842
by: lptl | last post by:
I know the title is misleading. I am working on a class project where we are trying to setup a notification system written in PHP and using an MySQL database. We are trying to set-up a notification page that will show what users are nearing expiration. The expiration dates are entered in manually and are not a timestamp. The dates are entered in on another page into the SQL database. Then what we need is to be able to take those dates and find...
2
5412
by: barronmo | last post by:
I'm trying to get the difference in dates using the time module rather than datetime because I need to use strptime() to convert a date and then find out how many weeks and days until that date. I'm a beginner so any help would be appreciated. Here is the code: def OBweeks(ptID): qry = 'SELECT short_des FROM problems WHERE patient_ID = %s;' % (ptID) results = EMR_utilities.getAllData(qry) for items in results:
0
8596
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8561
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8240
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7038
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6072
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5527
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4042
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1692
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.