PHP Get How Many Days and Hours Left from a Date

Php get how many days and hours left from a date

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.

PHP: Days, hours and minutes left to a certain date?

Simply use DateTime class like as

$Draw_time = "29/01/2016 7pm";

$date = DateTime::createFromFormat("d/m/Y ha",$Draw_time);
$date2 = new DateTime();

echo $diff = $date2->diff($date)->format("%a days and %H hours and %i minutes and %s seconds");

days, hours, and minutes remaining in php

Always use DateTime

$create_time = "1496592689";
$current_time = time();

$dtCurrent = DateTime::createFromFormat('U', $current_time);
$dtCreate = DateTime::createFromFormat('U', $create_time);
$diff = $dtCurrent->diff($dtCreate);

$interval = $diff->format("%y years %m months %d days %h hours %i minutes %s seconds");
$interval = preg_replace('/(^0| 0) (years|months|days|hours|minutes|seconds)/', '', $interval);

echo $interval;

result

 6 months 30 days 5 hours 52 minutes

PHP Countdown to Date

You can use the strtotime function to get the time of the date specified, then use time to get the difference.

$date = strtotime("December 3, 2009 2:00 PM");
$remaining = $date - time();

$remaining will be the number of seconds remaining. Then you can divide that number to get the number of days, hours, minutes, etc.

$days_remaining = floor($remaining / 86400);
$hours_remaining = floor(($remaining % 86400) / 3600);
echo "There are $days_remaining days and $hours_remaining hours left";

Finding the number of days between two dates

$now = time(); // or your date as well
$your_date = strtotime("2010-01-31");
$datediff = $now - $your_date;

echo round($datediff / (60 * 60 * 24));

How to display days and hours with date() in php

function convert_seconds($seconds) {
$dt1 = new DateTime("@0");
$dt2 = new DateTime("@$seconds");
return $dt1->diff($dt2)->format('%a days, %h hours, %i minutes and %s seconds');
}
echo convert_seconds(200000)."\n";

Sample output:

2 days, 7 hours, 33 minutes and 20 seconds

In your case you should use this function like convert_seconds($job_expiration)

Calculate number of days remaining

$future = strtotime('21 July 2012'); //Future date.
$timefromdb = //source time
$timeleft = $future-$timefromdb;
$daysleft = round((($timeleft/24)/60)/60);
echo $daysleft;

How do I find the hour difference between two dates in PHP?

You can convert them to timestamps and go from there:

$hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1);

Dividing by 3600 because there are 3600 seconds in one hour and using round() to avoid having a lot of decimal places.

Display Days/Hours Until Certain Time

You can use the relative time format next Sunday 17:00. Like this:

$now = new DateTime();
$future_date = new DateTime('next Sunday 17:00');
$interval = $future_date->diff($now);
echo $interval->format("%d days, %h hours, %i minutes, %s seconds");

Output:

6 days, 2 hours, 33 minutes, 53 seconds

Read more about relative time formats here: http://www.php.net/manual/en/datetime.formats.relative.php



Related Topics



Leave a reply



Submit