Date / time operations (add, sub, diff..)

Hello,

I’d like to know where it is, in your opinion, the best way to do operations like the DATE_ADD, DATE_SUB, DATE_DIFF of mysql (http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html) using Yii.

As Yii as a multi database database abstraction layer I’d prefer to do not rely on mysql functions… so how else could I obtain the same result? In database I have a date or datetime field, not a timestamp.

thanks.

bye,

Giovanni.

If you don’t want to use MySQL specific functions, you could use the new DateTime class in PHP. It provides a lot of utilities for working with dates and times (even though the docs are not really complete yet).

hi,

thanks.

the new DateTime class is exaclty what I was looking for ;) was not aware of this new addition :P

by the way… I’ve saw that most of the methods are for php >= 5.3.0 only but you can use the modify method with 5.2.0+ ;)


<?php

$date = new DateTime("2006-12-12");

$date->modify("+1 day");

echo $date->format("Y-m-d");

?>

bye,

Giovanni.

p.s.

reading the documentation of this new class I’ve also found another method that could be used in most situations:


$mydate =date('Y-m-d', strtotime('2006-12-12 +1 month')); // = 2007-01-12

$mydate =date('Y-m-d', strtotime('+1 month')); // = now + 1 month = 2009-11-23