Thursday, 30 July 2015

Adding and Subtracting Dates and Times in PHP

$today = date(‘d/m/Y’);  // Todays date in date/month/4 digit year format

$yesterdaydt=date(‘d/m/Y’,strtotime(“-1 days”));  // GET Yesterdays Date


Subtracting days from a date

The following example will subtract 3 days from 1998-08-14. The result will be 1998-08-11.

$date = "1998-08-14";                                    
$newdate = strtotime ( '-3 day' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );                  
echo $newdate;                                           
 
Subtracting Weeks from a date

The following example will subtract 3 weeks from 1998-08-14. The result will be 1998-07-24. Notice that the only difference in the code is the week statement.

$date = "1998-08-14";                                     
$newdate = strtotime ( '-3 week' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );                   
echo $newdate;                                            
 
Subtracting Months from a date

The following example will subtract 3 months from 1998-08-14. The result will be 1998-05-14. Notice that the only difference in the code is the month statement.

$date = "1998-08-14";                                      
$newdate = strtotime ( '-3 month' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );                    
echo $newdate;                                             
 
Subtracting Years from a date

The following example will subtract 3 years from 1998-08-14. The result will be 1995-08-14. Notice that the only difference in the code is the year statement.

$date = "1998-08-14";                                     
$newdate = strtotime ( '-3 year' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );                   
echo $newdate;                                            
 
Adding days, months, weeks and years from a date

There isn’t really much difference from subtracting and adding dates. To add dates, just use any of the examples above and replace the negative (-) with a positive (+) e.g. ‘+3 weeks’
You could also just do:
echo date(“Y-m-j”, strtotime(“1998-08-14 -3 days”));
(sub days, weeks, months, as needed)

This adds 2 days to the current date:
 
$date = date('Y-m-j');                                   
$newdate = strtotime ( '+2 day' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );                  
echo $newdate;                                           
 
This adds 2 months to the current date:
 
$date = date('Y-m-j');                                     
$newdate = strtotime ( '+2 month' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );                    
echo $newdate;                                             
 
This adds 2 years to the current date:
 
$date = date('Y-m-j');                                    
$newdate = strtotime ( '+2 year' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );                   
echo $newdate;                                            

This subtracts 6 hours from the current date:
 
$date = date('Y-m-j');                                    
$newdate = strtotime ( '-6 hour' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );                   
echo $newdate;                                            

This subtracts 10 minutes from the current date:
 
$date = date('Y-m-j');                                       
$newdate = strtotime ( '-10 minute' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );                      
echo $newdate;                                               

If you want to change if from the current date, then simple replace the current date with the desired one…

This adds 1 year to the date 2012-11-25:
 
$date = date('2011-11-25');                               
$newdate = strtotime ( '+1 year' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );                   
echo $newdate;                                            

Get the Week Start Date and ENd date 

Consider today is a saturday for the given example

$day = date('w');                                                             // Gives Day of week, returns 6 if it is saturday
$sday = $day - 1;                                                            // I need only working dates mon-sat so 6-1=5 
$eday = 6 - $day;                                                            // Excepting sunday in the list                           
$ws = date('d/m/Y', strtotime('-'.$sday.' days'));            // -5 days to current date that is monday            
$we = date('d/m/Y', strtotime('+'.$eday.' days'));          // + 0 days  to current date that is today - sat      
$lastsunday = date('d/m/Y',strtotime('-'.$day.' days'));  // Gives last week sunday date                           
$comingsunday = date('d/m/Y',strtotime('+'. 7-$day.' days')); // coming sunday                                    

like wise we can get any day of the week , note that the return result is a string

Code for Subtracting a day from given date 

$date = '09/02/2015';
$day_before = date( 'm/d/Y', strtotime( $date . ' -1 day' ) );
echo "<br/><br/>";
$db = date('d/m/Y',strtotime($day_before));

echo $db;

No comments:

Post a Comment