TrinityTuts Tips

Working with date in PHP

Here is a simple tip if you are working with Date and time in php.
PHP provide some method which are really very helpful to help you. We commonly have two different method which very powerful to calculate difference between date.

Method 1. The easiest and most common method is

strtotime(time());

 Method 2. Second method is latest and i prefer this.

 new DateTime(time());

in this you need to pass your date and you get your response in object like this

DateInterval Object
(
    [y] => 3
    [m] => 5
    [d] => 15
    [h] => 0
    [i] => 0
    [s] => 0
    [invert] => 0
    [days] => 1264
)

Example: This is simple example using new DateTime() in this i simply compare between two dates

<?php
$date1 = new DateTime('May 13th, 2015');
$date2 = new DateTime('October 15th, 2015');

$difference = $date1->diff($date2);