PHP: working with dates and time

Difference in days between two dates

This function will calculate the difference between two given dates. Input time by ISO 8601 standards (yyyy-mm-dd). i.e: daysBetweenDate(‘2009-01-01’, ‘2010-01-01’) will return 365.

function daysBetweenDate($from, $till) {
  if ($till < $from) {
    trigger_error("The date till is before the date from", E_USER_NOTICE);
  // Explode date since gregoriantojd() requires mm, dd, yyyy input
  $from = explode('-', $from);
  $till = explode('-', $till);
  // Calculate date to Julian Day Count
  $from = gregoriantojd($from[1], $from[2], $from[0]);
  //Calculate date to Julian Day Count
  $till = gregoriantojd($till[1], $till[2], $till[0]);
  // Substract the days $till from $from to get the amount of days
  return $till - $from;

Get current year minus 10 years

$past_year = date("Y", strtotime("-10 year", time()));

Leave a Reply