A few helpful PHP date methods

For a lot of people dates in PHP are an issue, and the PHP functions aren’t sufficient enough to do all converts you need.
Therefore you find a few easy PHP date conversion methods. Feel free to add your own methods or requests in the comments as we know that these methods don’t cover everything. You can see it for yourself when you read the rest of this entry:

  • get start and end date for the next two months in an array
  • get the datetime for a specific user culture in Symfony
  • get all days between a given start and end date and return it in an array
  • get the difference between two dates

  1. get start and end date for the next two months in an array
    input
    : nothing
    output:
    array[0] = 1239706452
    array[1] = 1244976852
    *updated -> thx naholyr
    /**
     * get the next two months starting from today.
     *
     * @return array
     */
     public function getNextTwoMonths(){
     return array(time(), strtotime(’+2 month’));
     }
    
  2. get the datetime for a specific user culture in Symfony
    input
    : 16/09/08
    output
    : 2008-09-16 23:35:55
     /**
     * Parses a date String from a specific culture to a Date.
     *
     * input example: 16/09/08
     *
     * returns: DateTime (Date)
     * @param String $period
     * @return Array of DateTime objects
     */
     public function parseDate($period) {
    
     $userCulture =  sfContext::getInstance()->getUser()->getCulture();
    
     $timestamp = sfContext::getInstance()->getI18N()->getTimestampForCulture($period, $userCulture);
    
     $period = new DateTime(date('Y-m-d H:i:s', $timestamp));
    
     return $period;
     }
    
  3. get all days between a given start and end date and return it in an array.
    input
    : 2 DateTime dates
    output
    : array of days
    day[0] = 2008-12-10
    day[1] = 2008-12-11
    /**
     * With a given start and end date it will creates all the dates in between and return
     * it in an array.
     *
     * @param DateTime $startDate
     * @param DateTime $endDate
     * @return Array
     */
     public function getDaysByPeriod(DateTime $startDate, DateTime $endDate) {
    
     $startDt = clone $startDate;
    
     $days = array();
    
     while ( $startDt->format('U') <= $endDt->format('U') ) {
     $days[] = $startDt->format('Y-m-d');
     $startDt->modify("+1 days");
     }
    
     return $days;
     }
    
  4. get the difference between two dates
    input
    :
    2009-04-14 10:08:35
    2009-04-15 10:10:35
    output: unix timestamp of the difference
    * updated -> thx Toni and David
    /**
     * get the difference between two dates
     *
     * @param date $firstTime
     * @param date $lastTime
     * @return integer
     */
    public function timeDiff($firstTime,$lastTime)
    {
     return strtotime($lastTime) - strtotime($firstTime);
    }
    
This entry was posted in General, php, Symfony and tagged , , , , . Bookmark the permalink.

8 Responses to A few helpful PHP date methods

  1. David says:

    Regarding the last one: why don’t you use strtotime?

    return strtotime($lastTime) – strtotime($firstTime);

  2. Michael says:

    There is also a very handy plugin for easy advanced date manipulation http://www.symfony-project.org/plugins/sfDateTime2Plugin

  3. David :

    Regarding the last one: why don’t you use strtotime?

    return strtotime($lastTime) – strtotime($firstTime);

    In our case we had this format ’2009-05-10 23:10:35′ as input, and you cannot do that with the strtotime method, but it is true that there are different ways to do substractions.

  4. van Rumste Kenneth :
    In our case we had this format ‘2009-05-10 23:10:35′ as input, and you cannot do that with the strtotime method, but it is true that there are different ways to do substractions.

    David is right, it does work perfectly with strtotime.

    $time = strtotime(’2009-05-10 23:10:35′);
    echo date(‘Y-m-d H:i:s’, $time);

    This will output the same date.

  5. naholyr says:

    getNextTwoMonths() is false : you add 60 days, and +2 Months will (almost) never be +60 days.

    “return array(time(), strtotime(‘+2 month’))” woule be true.

  6. David says:

    naholyr :
    getNextTwoMonths() is false : you add 60 days, and +2 Months will (almost) never be +60 days.

    Not only that. If you use this during September-October it will return 59 days because of the extra hour when DST is deactivated again (during that range 60 days = 60 * 24h + 1h).

    @Kenneth and Toni: that’s pure ISO8601 date format and this one always works with all of PHP’s date functions.
    Btw, in getDaysByPeriod() I see no need to clone $endDate because it isn’t modified anyway.

    And since I haven’t said it yet: thanks though for all these examples, I’ve had hard times with dates&times myself (but since I’ve had to use them so much I’m quite comfortable with them by now) and it’s very helpful – not only for newcomers.

  7. Pingback: Daily Digest for 2009-04-15 | Pedro Trindade

  8. Pingback: A few helpfull PHP date methods | devexp - 【23php】

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">