简体   繁体   中英

Default Date Time Format date_diff

date_diff not support d/m/Y format. Currently I using strtotime to convert date to m/d/Y but I want to know the way to change date_diff date format to d/m/Y ?

Thank you.

Use date_create along with date_diff

$datetime1 = date_create('10/11/2009');
$datetime2 = date_create('10/13/2009');
$interval = date_diff($datetime1, $datetime2);

Ref: http://php.net/manual/en/datetime.diff.php

You can use DateTime::createFromFormat :

$date1 = DateTime::createFromFormat( 'd/m/Y', '14/05/2016' );  # oo style
$date2 = date_create_from_format( 'd/m/Y', '17/05/2016' );     # procedural style

$dateInterval = $date1->diff( $date2 );

or:

$dateInterval = date_create_from_format( 'd/m/Y', '14/05/2016' )->diff( date_create_from_format( 'd/m/Y', '17/05/2016' ) );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM