A few helpful PHP date methods

Warning! This post is either deprecated or outdated.

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 <pre class="brush: php; title: ; notranslate" title="">/
    • get the next two months starting from today. *
    • @return array */ public function getNextTwoMonths(){ return array(time(), strtotime(’+2 month’)); }

</pre>

  1. get the datetime for a specific user culture in Symfony
    input
    : 16/09/08
    output
    : 2008-09-16 23:35:55 <pre class="brush: php; title: ; notranslate" title="">/**
    • 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; } </pre>

  1. 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 <pre class="brush: php; title: ; notranslate" title="">/**
    • 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; } </pre>

  1. 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 <pre class="brush: php; title: ; notranslate" title="">/**
    • get the difference between two dates *
    • @param date $firstTime
    • @param date $lastTime
    • @return integer */ public function timeDiff($firstTime,$lastTime) { return strtotime($lastTime) - strtotime($firstTime); }

</pre>

By the way, if you found a typo, please fork and edit this post. Thank you so much! This post is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

Comments

Fork me on GitHub