LATEST >>

Welcome Here And Thanks For Visiting. Like Us On Facebook...

EXEIdeas – Let's Your Mind Rock » HTML-CSS-PHP-JavaScript / PHP Codes » How To Get Remaining Days, Hours, Minutes And Seconds From A Date Using PHP?

How To Get Remaining Days, Hours, Minutes And Seconds From A Date Using PHP?

How-To-Get-Remaining-Days,-Hours,-Minutes-And-Seconds-From-A-Date-Using-PHP
Today, we will learn to calculate the time remaining till future dates in days, hours, minutes and seconds. Either you are receiving values in the PHP variable from the MySQL database, or you are calling an AJAX request, this tutorial will help you in both situations. We will also display the hours, minutes and seconds in leading zeroes.

Basically, we are going to get the difference between the current date and the date in future. Then we will extract the days, hours, minutes and hours from that difference. The difference will be in seconds so we have to apply some math to it. First, we assume that you are already receiving the value from your MySQL database in the PHP variable. We have set the PHP variable below but you can use your own database value:

Features:

  1. Light Weight.
  2. Pure PHP Code.
  3. Expired Check Also On Over Date/Time.
  4. No External Files.
  5. Work With Any Format Of Date.
  6. Fully Customizable.

How To Get Remaining Days, Hours, Minutes And Seconds From A Date Using PHP?

There are a few easy and understandable steps to achieve your desired functionality using pure PHP that we are gonna share below. Follow each step perfectly.

Recommended For You:
How To Center A DIV Vertically And Horizontally By Valid CSS?

PHP:

if (strtotime(date("Y-m-d")) > strtotime($row['ExpiryDate'])){
$expired = true;
} else {
$expired = false;
$seconds = strtotime($row['ExpiryDate']) - time();
$days = floor($seconds / 86400);
$seconds %= 86400; 
$hours = floor($seconds / 3600);
$seconds %= 3600; 
$minutes = floor($seconds / 60);
$seconds %= 60;
}
echo ($expired==true?"Expired":"$days days and $hours hours and $minutes minutes and $seconds seconds remaining");

Customization:

No need to customize it. Just copy-paste. Rest edit the code as per comments and need.

Troubleshooting the Errors:

Do it with concentration and patience. Check your all steps and again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.

Final Words:

That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts and problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave your precious feedback in our comment form below. Happy development, See you in the next article.

You Like It, Please Share This Recipe With Your Friends Using...

Be the first to write a comment.

Leave a Reply

Your email address will not be published.