How to compare two dates

I have a MySQL database with a PHP interface. In my records, I have a publication date and an expiration date directly accessible from the database. What I need to do is check and see if the expiration dates of the date sent by date are the same.

Something like:

<?php $posted_date= $row_Recordset1['date_posted']; ?> <?php $exp_date= $row_Recordset1['expire_date']; ?> <?php if ($posted_date("Ymd") >= $exp_date("Ymd")) { //statement <?php } ?> 
+2
date php
Sep 30 '09 at 6:54
source share
2 answers

You can do it:

 $posted_date= $row_Recordset1['date_posted']; $exp_date= $row_Recordset1['expire_date']; if (strtotime($posted_date) >= strtotime($exp_date)) { // Do whatever } 

This will work if you assume that the dates from the database are standard date string strings.

+1
Sep 30 '09 at 7:30
source share

You can turn them into Unix timestamps using strtotime , assuming they start as a string, and then they will be just integers that you can compare. Another option would be to use DateTime objects that can be compared using comparison operators. If your date is in strtotime format, you can do $dt=new DateTime($row_Recordset1['expire_date']);

+1
Sep 30 '09 at 7:02
source share



All Articles