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. DateTime's object-oriented interface encapsulates a lot of back-end logic and provides a clean interface for us to use. Whereas with strtotime, we need to convert dates to timestamps and compare them. Comparison of two date is direct with DateTime. Working with object is just easier than arbitrary functions.DateTime is able to process more date string formats comparing to strtotime.Most of people have been ignoring it, despite it has been available since PHP 5.2.Ī couple of reasons why you should use DateTime over strtotime and date: PHP provides a dedicated class DateTime for dealing with date and time. If you are still using functions like strtotime and date to work with date and time, you are missing out. It's a common task to work with date and time as a web developer.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |