date_sub is not working
As my server is not upgreaded to php 5.3 (it is in PHP Version 5.2.11) date_sub is not working.
here is the code: $date = date_create(date('Y-m-d'));
date_sub($date, date_interval_create_from_date_string('60 days'));
$date1= date_format($date, 'Y-m-开发者_JS百科d');
but its ok in my localhost(which is in 5.3), but not in server(5.2.11).
Can you please tell me how can i make this date subtraction working on 5.2.11 ?
Like the docs say, use DateTime::modify()
.
$date1= date('Y-m-d', strtotime("-60 days"));
it works :)
For adding and subracting days
$newdate = date('Y-m-d', strtotime("-10 days"));
echo $newdate;
sub 10 days
$newdate1 = date('Y-m-d', strtotime("+10 days"));
echo $newdate1;
add 10 days
For adding and subracting months
$newmonth = date('Y-m-d', strtotime("-5 months"));
echo $newmonth;
sub 10 months
$newmonth1 = date('Y-m-d', strtotime("+5 months"));
echo $newmonth1;
add 10 months
As the manual page says, this function works only for the PHP version >= 5.3.0.
So, you've found right solution yourself
for EX $da=2014-04-01
if u want to minus 6 months use this..
$date = strtotime($da .' -6 months');
$final=date('Y-m-d', $date);
echo $final;
精彩评论