Comparing dates in PHP

In our barangay system, there are only three (3) persons who can login in the system namely the Secretary, Treasurer and Book Record Keeper. One feature in this system is that if their term in their positions will come to and end, they cannot login in the system anymore. The crucial thing in making this feature is to compare Dates. Comparing dates is not easy but hopefully this can help you if ever you encounter this.
I tried using date_diff in comparing dates in reference to this website but it won’t work even if I copied the example and always getting this error

“date_diff() expects parameter 1 to be datetime”.

After several hours, I finally had the solution.



This is where the magic happens.

The variable $endTerm contains the date that is from MySQL then is converted to date which is in the variable $e_D. $e_c_D contains the current date. After that, these to variables needs to be prepared for comparison so date_parse is needed. date_parse returns an associative array. After that you can already use the variables $expiry_date and $current_date for comparison.





2 Comments Add yours

  1. or if you use datetime as your datatype, then you can up-convert it to a timestamp via strtotime(), then explicit cast to integer then take its differences from the current timestamp.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s