Tutorial :What PHP function(s) can I use to perform operations on non-integer timestamps?


Disclaimer, I'm not a PHP programmer, so you might find this question trivial. That's why I'm asking you!

I've got this kind of timestamp: 2010-05-10T22:00:00 (That's Y-m-d)

I would like to subtract, say, 10 days (or months, whatever) from this, and have my result be in the same format, i.e. 2010-04-30T22:00:00.

What function(s) do I need to do this in PHP?

Note: I'm using this to do a computed field in Drupal. The result will be the date that an e-mail is sent.

Bonus question: If 2010-05-10T22:00:00 means "May 10, 2010 at 10pm", is there a timestamp equivalent of "May 10, 2010 (all day)"?


You must use datetime class: http://php.net/manual/en/book.datetime.php

DateTime Diffrence: http://www.php.net/manual/en/datetime.diff.php

DateTime Substraction: http://www.php.net/manual/en/datetime.sub.php

Bonus answer: Yes you can do this, you must convert your date to timestamp using DateTime() twice (1. With 00:00:00 time, and with 23:59:59 time), when you compare this timestamps to your orginal (maybe middle) timestamp, and you have answer from compare results.


To enable any operations on a date value, you first need to create a DateTime object. In PHP 5.3 an later you could use DateTime::createFromFormat(). In earlier versions of PHP you'd need to create a DateTime object with new DateTime();.

The manipulations that are possible with a DateTime object are also listed in the manual here.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »