Forum Moderators: coopster

Message Too Old, No Replies

subtracting time from a TIMESTAMP

         

MWpro

2:57 am on Mar 31, 2009 (gmt 0)

10+ Year Member



How might one do the follow in PHP:

Get the current time in TIMESTAMP form.

Subtract "x" amount of minutes from that timestamp. This also needs to be in TIMESTAMP form when all is said and done.

Thanks!

blang

5:24 am on Mar 31, 2009 (gmt 0)

10+ Year Member



Are you referring to a UNIX TIMESTAMP, e.g. the number of seconds from the "epoch" (Jan 1, 1970 00:00:00UTC)?
It's just in seconds, so figure out how many seconds X represents and subtract it from the current UNIX TIMESTAMP value.

[edited by: blang at 5:25 am (utc) on Mar. 31, 2009]

coopster

3:27 pm on Mar 31, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If it is in TIMESTAMP form, can we assume it is coming from a database? If so, I would use the database to do the subtraction.
SELECT '2009-03-31 10:30:00' - INTERVAL 1440 MINUTE; 
// 2009-03-30 10:30:00

MWpro

10:08 pm on Mar 31, 2009 (gmt 0)

10+ Year Member



Thank you for your responses.

It will not originally come from the database, it needs to come from the current time; however, after the subtraction it will end up being put into a database.

coopster

10:27 pm on Mar 31, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



So if it is coming from the current time then you know it is not user-supplied, it's just right off your system -- use the example above to populate the field in your database.
"INSERT INTO table (myTimeColumn) VALUE(CURRENT_TIMESTAMP - INTERVAL $minutes MINUTE)"

MWpro

3:15 am on Apr 1, 2009 (gmt 0)

10+ Year Member



Thank you!