Last active
November 5, 2015 11:39
-
-
Save pzzrudlf/2e97f38645ce7d59fa16 to your computer and use it in GitHub Desktop.
This file contains 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 | |
$time1 = "2015-10-03 12:02:11"; | |
$time2 = date('Y-m-d H:i:s',strtotime("$time1 -2 year")); | |
echo $time2."<br>";//"2013-10-03 12:02:11" | |
$time3 = '2015'; | |
$time4 = date('Y',strtotime("$time3 -3 year")); | |
echo $time4."<br>";//"2012" | |
$time5 = '2015-01-01'; | |
$time6 = date('Y',strtotime("$time4 -2 year")); | |
echo $time6."<br>";//"2013" | |
$time7 = '2015'; | |
$time8 = date('Y',strtotime("-1 year",time($time7))); | |
echo $time8."<br>";//"2014" | |
$time9 = '2015-01-01'; | |
$time10 = date('Y',strtotime("$time9 -10 year")); | |
echo $time10."<br>";//"2005" | |
$time10 = '2014-01-01'; | |
$time11 = date('Y',strtotime("$time10 -20 year")); | |
echo $time11."<br>";//"1994" | |
/* | |
* $time9 is much better than $time7. | |
* | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note:
The "Year (and just the year)" format only works if a time string has already been found -- otherwise this format is recognised as HH MM.
http://php.net/manual/en/datetime.formats.date.php