php date compare or Calculate the difference between two dates. Here we have declared two dates date1anddate2 in the same format. $diffrence = $today->diff($date); //enddate is “01-01-2022”
php date compare – Comparing two dates in PHP
Contents
PHP DateTime: Create, Compare and Format Dates Easily. i have used a comparison operator(>) to compare the dates.
Method 1: How to compare two dates in PHP
<?php $join_dt = "2022-01-15"; $end_dt = "2032-12-14"; if ($join_dt < $end_dt) echo "$join_dt is less than $end_dt"; else echo "$join_dt is greater than $end_dt"; ?>
Result
2022-01-15 is less than 2032-12-14
Method 2: How to compare two dates in PHP
<?php $join_dt = "2022-01-15"; $end_dt = "2032-12-14"; $timestamp1 = strtotime($join_dt); $timestamp2 = strtotime($end_dt); if ($timestamp1 > $timestamp2) echo "$join_dt is greater than $end_dt"; else echo "$join_dt is less than $end_dt"; ?>
Comparing Two Dates In PHP Examples
Result
2022-01-15 is less than 2032-12-14
Comparing two dates in PHP
php date compare with today
<?php $join_dt = new DateTime('2032-12-11 12:12:00'); $end_dt = new DateTime('2032-12-13 10:12:00'); if ($join_dt > $end_dt) { echo 'join_dt greater than end_dt'; } if ($join_dt < $end_dt) { echo 'join_dt lesser than end_dt'; } if ($join_dt == $end_dt) { echo 'end_dt is equal than join_dt'; }
I hope you get an idea about php date compare.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, donโt forget to share.
I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I’m a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.