Skip to content

Instantly share code, notes, and snippets.

@pzzrudlf
Last active November 5, 2015 11:39
Show Gist options
  • Save pzzrudlf/2e97f38645ce7d59fa16 to your computer and use it in GitHub Desktop.
Save pzzrudlf/2e97f38645ce7d59fa16 to your computer and use it in GitHub Desktop.
<?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.
*
*/
@pzzrudlf
Copy link
Author

pzzrudlf commented Nov 5, 2015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment