Last active
October 4, 2018 04:40
-
-
Save tieutantan/0a36614b4a93689d7dd330171b396b56 to your computer and use it in GitHub Desktop.
PHP time manipulation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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