Tag Archives: datetime

If DateTime Object Is Null

Questions: I am returning some search results after form submit. All is working fine, until I get to a field that is SMALLDATETIME and allows NULL. At least one of the returned rows has the field as NULL. As you can see in the code below, when it is not NULL I am converting it… Read More »

How to get aggregate days from PHP's DateTime::diff?

Questions: $now = new DateTime(‘now’); $tomorrow = new DateTime(‘tomorrow’); $next_year = new DateTime(‘+1 year’); echo “<pre>”; print_r($now->diff($tomorrow)); print_r($now->diff($next_year)); echo “</pre>”; DateInterval Object ( [y] => 0 [m] => 0 [d] => 0 [h] => 10 [i] => 17 [s] => 14 [invert] => 0 [days] => 6015 ) DateInterval Object ( [y] => 1 [m]… Read More »

Wrong month (February) – DateTime::createFromFormat

Questions: The date conversion from string returns wrong values for the 2nd month (February): $dtformat = ‘Y-m-01’; $curDate = DateTime::createFromFormat(‘Y-m’, ‘1996-02’); print_r($curDate); $dt = $curDate->format($dtformat); echo $dt.”\n”; Instead of “1996-02-01”, it returns “1996-03-01”. This is the $currDate array: DateTime Object ( [date] => 1996-03-02 01:19:01 [timezone_type] => 3 [timezone] => America/New_York ) All other months… Read More »

How to detect Ambiguous and Invalid DateTime in PHP?

Questions: When dealing with local DateTime values provided by a user, it’s quite possible to have a time that is either invalid or ambiguous, due to Daylight Saving Time transitions. In other languages and frameworks, there are often methods such as isAmbiguous and isValid, on some representation of the time zone. For example in .NET,… Read More »

add one year to datetime with php

Questions: $data[‘user’][‘time’] = ‘2011-03-07 00:33:45’; how can we add 1 year to this date ? something like $newdata = $data[‘user’][‘time’] + 1 year ? or $newdata = 2012-03-07 00:33:45 Thanks Adam Ramadhan Answers: