note 126177 added to function.date-diff
| From: | jab_creations.yahoo.com via php-notes | Date: | Fri, 18 Jun 2021 01:04:20 +0000 |
| Subject: | note 126177 added to function.date-diff | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-216530@lists.php.net to get a copy of this message | ||
PHP is usually relaxed when it comes to data types however that is not the case with date and time.
If all you need to do is compare a string date against today's date you need to do the
following:
<?php
//Create a date object out of a string (e.g. from a database):
$date1 = date_create_from_format('Y-m-d', '2026-05-10');
//Create a date object out of today's date:
$date2 = date_create_from_format('Y-m-d', date('Y-m-d'));
//Create a comparison of the two dates and store it in an array:
$diff = (array) date_diff($date1, $date2);
//Output the array:
echo '<pre>'.print_r($diff,1).'</pre>';
?>
This will output the following:
Array
(
[y] => 4
[m] => 10
[d] => 22
[h] => 0
[i] => 0
[s] => 0
[f] => 0
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 1
[days] => 1787
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)
So you can simply use:
<?php
echo $diff['days'];
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.242 (proxied: 154.21.22.124)
----
Manual Page -- https://php.net/manual/en/function.date-diff.php
Edit -- https://main.php.net/note/edit/126177
Del: integrated -- https://main.php.net/note/delete/126177/integrated
Del: useless -- https://main.php.net/note/delete/126177/useless
Del: bad code -- https://main.php.net/note/delete/126177/bad+code
Del: spam -- https://main.php.net/note/delete/126177/spam
Del: non-english -- https://main.php.net/note/delete/126177/non-english
Del: in docs -- https://main.php.net/note/delete/126177/in+docs
Del: other reasons-- https://main.php.net/note/delete/126177
Reject -- https://main.php.net/note/reject/126177
Search -- https://main.php.net/manage/user-notes.php