Skip to content

Instantly share code, notes, and snippets.

@adamthebig
Forked from ZellSnippets/gist:8857254
Created February 28, 2016 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamthebig/f66f750f90e482628649 to your computer and use it in GitHub Desktop.
Save adamthebig/f66f750f90e482628649 to your computer and use it in GitHub Desktop.
PHP: DateTime common
<?
// Get Year
$today = new DateTime();
$year = $today->format('Y');
// Set Date
$date = new DateTime();
$date->setDate(2014, 12, 31);
// Echo date
echo $date->format('Y-m-d');
// Get Time difference
$interval = $today->diff($end_of_year);
echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";
echo "difference " . $interval->days . " days ";
// Get No of days in a year
// set date for both start and end of year, then use the following
$interval = $end_of_year->diff($start_of_year);
$days_in_year = $interval->days + 1;
echo $days_in_year;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment