Skip to content

Instantly share code, notes, and snippets.

@tieutantan
Last active October 4, 2018 04:40
Show Gist options
  • Save tieutantan/0a36614b4a93689d7dd330171b396b56 to your computer and use it in GitHub Desktop.
Save tieutantan/0a36614b4a93689d7dd330171b396b56 to your computer and use it in GitHub Desktop.
PHP time manipulation
<?php
// counter 2018-09-23 10:01:27 to days old
function days_old($time)
{
$date = strtotime($time); # Converted to a PHP date (a second count)
$diff = $date - time(); # time returns current time in seconds
$days = floor($diff / (60 * 60 * 24)); # seconds/minute*minutes/hour*hours/day)
return abs($days); // number of days old
}
// 2018-09-23 10:01:27 add 5 days to 2018-09-28 10:01:27
function add_days_old($current_time, $days_add)
{
$timestamp = strtotime($current_time); # Converted to a PHP date (a second count)
$timestamp = $timestamp + $days_add * (60 * 60 * 24); # add more time (a second count)
return date('Y-m-d H:i:s', $timestamp); // example 2018-09-23 10:01:27
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment