DateInterval's constructor accepts a string that contains a valid interval specification.Īn intercept of explanation for interval specification from PHP official site: The only tricky part may be creating a DateInterval object. Most of the code above is easy to understand. PHP_EOL $today -> add ( new DateInterval ( 'P2D' )) echo $today -> format ( 'Y-m-d' ). Let's demonstrate how addition works in a simple example: $today = new DateTime ( 'today' ) echo $today -> format ( 'Y-m-d' ). DateTime::add accepts a single DateInterval object parameter. #AdditionĪdding to an existing DateTime is done through function DateTime::add. Though in reality, we can never manipulate time, with DateTime, we can actually do that. There are a number of interval properties in DateInterval class, you can check them out at PHP official site. We can directly compare two DateTime objects just like two numeric values.Ī few examples of comparing two DateTime objects: $today = new DateTime ( 'today' ) $yesterday = new DateTime ( 'yesterday' ) var_dump ($today > $yesterday) var_dump ($today diff ($yesterday) echo $interval -> format ( '%d day ago' ) // Output 1 day ago When working with date string, we need to convert them to timestamp using strtotime in order to compare their value.ĭateTime objects work with comparison operators out of box. We can create any desired format, because the format parameter provides all the possible options.Ī few more examples: print_r ($now -> format ( 'jS F Y' )) print_r ($now -> format ( 'ga jS M Y' )) print_r ($now -> format ( 'Y/m/d s:i:H' )) // Output: 6th January 2016 1am 6th Jan 2016 2016 / 01 / 06 11 : 39 : 01 #Comparison This string should contain options listed at PHP official page.įor example, to get a yyyy-dd-mm format outputted by DateTime, we can do: $now = new DateTime () $ymdNow = $now -> format ( 'Y-m-d' ) ![]() It's done via DateTime's format() method.ĭateTime::format() accepts a single string parameter. We will always want a specific format depends on the system we are building.įormatting a DateTime object to get a custom string representation is straightforward. Of cause, we can also create a DateTime object with traditional date string: $dateTime = new DateTime ( ' 12:30:12' ) #Format This parameter accepts a DateTimeZone object only.įor example, to create a DateTime object with Singapore timezone: $yesterdayInSingapore = new DateTime ( 'yesterday', new DateTimeZone ( 'Singapore' )) The second parameter of the DateTime's constructor allows us to specify a timezone. The default timezone will still be used since it is not specified.Ī few examples of creating DateTime objects with valid time string: $yesterday = new DateTime ( 'yesterday' ) $twoDaysLater = new DateTime ( '+ 2 days' ) $oneWeekEarly = new DateTime ( '- 1 week' ) Optionally we can supply a string, which represents a valid date and time, as the first parameter of the DateTime's constructor. The default timezone is normally configured in php.ini file.įor example, to create a "now" DateTime object: $currentDateTime = new DateTime () When no parameter is supplied, an DateTime object with current timestamp and the default timezone will be created. ![]() We instantiate an DateTime object via its constructor. Instantiating an DateTime object is no different from instantiating any other objects in PHP.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |