Change language

PHP DateTime diff () function

Syntax:
  • Object Oriented Style:
    DateInterval DateTime::diff (DateTimeInterface  $datetime2, bool $absolute = FALSE)
    or
    DateInterval DateTimeImmutable::diff (DateTimeInterface $datetime2, bool $absolute = FALSE)
    or
    DateInterval DateTimeInterface::diff (DateTimeInterface $datetime2, bool $absolute = FALSE)
  • Procedural style:
    DateInterval date_diff ( DateTimeInterface $datetime1, DateTimeInterface $datetime2, bool $absolute = FALSE)
Parameters:This function uses two parameters as above and described below:
  • $datetime:This parameter contains the date to compare with the first date.
  • $absolute:this parameter forces the spacing to be positive.
Returned from Meaning:This function returns the difference between two given DateTime objects.The following programs illustrate the DateTime::diff() function in PHP:Program 1:  
// Initialize two date and time objects $datetime1 = new DateTime ( ’ 2019-9-10’ ); $datetime2 = new DateTime ( ’2019-9-15’ );  
// Call diff() for above
// two DateTime objects $difference = $datetime1 -> diff ( $datetime2 );  
// Get the difference between the two
// these DateTime objects echo $difference -> format ( ’% R% a days’ ); ?>
Exit:
+5 days
Program 2:  
// Initialize two date and time objects $datetime1 = new DateTime ( ’2019-8-10’ ); $datetime2 = new DateTime ( ’2019-9-10’ );  
// Call the diff() function for the above
// two DateTime objects $difference = $datetime1 -> diff ( $datetime2 );  
// Get the difference between the two
// these DateTime objects echo $difference -> format ( ’% R% a days’ ); ?>
Exit:
+31 days
Link: https://www.php.net/ manual / en / datetime.diff.php

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method