Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php get how many days and hours left from a date

I have a created_at date saved liked this "2011-09-23 19:10:18" And I want to get the days and hours left until the date is reached. How do I do that? and column name in database remain days automatically update daily with remain days, please solve this

like image 477
John123 Avatar asked Sep 19 '11 17:09

John123


People also ask

How can I calculate hours between two dates in php?

You can convert them to timestamps and go from there: $hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1);

How can I get days between two dates in php?

The date_diff() function is an inbuilt function in PHP that is used to calculate the difference between two dates. This function returns a DateInterval object on the success and returns FALSE on failure.

How can I calculate total hours in php?

There are two ways to calculate the total time from the array. Using strtotime() function: The strtotime() function is used to convert string into the time format. This functions returns the time in h:m:s format. Example 1: This example reads the values from the array and converts it into the time format.

How can I get 30 days from date in php?

php $next_due_date = date('y-m-d',strtotime('+30 days',strtotime('echo $userRow3["due_date"]'))) .


2 Answers

PHP fragment:

<?php

//Convert to date
$datestr="2011-09-23 19:10:18";//Your date
$date=strtotime($datestr);//Converted to a PHP date (a second count)

//Calculate difference
$diff=$date-time();//time returns current time in seconds
$days=floor($diff/(60*60*24));//seconds/minute*minutes/hour*hours/day)
$hours=round(($diff-$days*60*60*24)/(60*60));

//Report
echo "$days days $hours hours remain<br />";
?>

Note the hour-round and no minutes/seconds consideration means it can be slightly inaccurate.

like image 80
Rudu Avatar answered Oct 21 '22 18:10

Rudu


This should seed your endeavor.

getdate(strtotime("2011-09-23 19:10:18"))

Full conversion:

$seconds = strtotime("2011-09-23 19:10:18") - time();

$days = floor($seconds / 86400);
$seconds %= 86400;

$hours = floor($seconds / 3600);
$seconds %= 3600;

$minutes = floor($seconds / 60);
$seconds %= 60;


echo "$days days and $hours hours and $minutes minutes and $seconds seconds";
like image 25
Korvin Szanto Avatar answered Oct 21 '22 19:10

Korvin Szanto