Skip to content

Instantly share code, notes, and snippets.

@UVLabs
Last active February 18, 2021 21:17
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 UVLabs/b98047abd622caa9380e713bd8249343 to your computer and use it in GitHub Desktop.
Save UVLabs/b98047abd622caa9380e713bd8249343 to your computer and use it in GitHub Desktop.
Get the number of days since a specific date or timestamp in PHP with timezone support.
<?php
$date_in_the_past = '@1613681176'; // If using timestamps be sure to include '@' symbol
$date_in_the_past_obj = new DateTime($date_in_the_past);
// If timezones matter for your usecase, then use below instead. Ref: https://www.php.net/manual/en/timezones.php
// $date_to_check = '2021-02-18';
// $date_to_check_obj = new DateTime($date_to_check, new DateTimeZone('America/St_Lucia'));
// You can add a different date that you want to compare instead of "today"
$date_in_the_future = new DateTime('today');
$date_difference = $date_in_the_past_obj->diff($date_in_the_future);
$days_since = (int) $date_difference->format('%a');
echo $days_since;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment