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
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.
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";
as of PHP 5.3.0 you could use build-in Date object:
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
http://www.php.net/manual/en/book.datetime.php
it would be something like
echo $date = date("Y-m-d H:i:s");echo "\n";
$original=time($date);
$modified = "2011-09-23 19:10:18";
echo date("Y-m-d H:i:s",$modified);echo "\n";
The easiest way is improved answer from CountZero. I used this solution for counter for time remaining before expiration of offer. Addition to first three lines of CountZero code:
$days = $interval->d;
$hours = $interval->h;
$minutes = $interval->i;
$seconds = $interval->s;
And now, you can use string functions to moderate your return values, to merge all or add '0' or '00' in front of the values.
You can find current date and time using date() function and then subtract
$x = 2011-09-23 - current_date
this will give you no. of days left.
Do same with time as well ..
Hope this helps
Related
I am looking a solution in Php.
I have a total time for a task in hh:mm:ss format. (Date is not important).
and I am entering each working time also in hh:mm:ss format. Now I have to find out the remain hours in hh:mm:ss format.
Example
16:00:00 is the assigned hours to do a task.
In different dates one employee worked 13:15:00 hours
Now I need the remaining hours as 2:45:00 in format.
If date is attached then we can do like
datetime1 = new DateTime('2014-02-11 04:04:26 AM');
$datetime2 = new DateTime('2014-02-11 05:36:56 AM');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%h')." Hours ".$interval->format('%i')." Minutes";
In my case there is no date. is there any functionality in PHP to find this type of hh:mm:ss difference?
Convert your HH:MM:SS format to a duration in seconds, subtract them, format them back to HH:MM:SS:
function str_to_seconds($duration) {
list($h, $m, $s) = explode(':', $duration);
return $s + ($m * 60) + ($h * 3600);
}
function seconds_to_str($seconds) {
return sprintf('%02d:%02d:%02d', $seconds / 3600, $seconds / 60 % 60, $seconds % 60);
}
echo seconds_to_str(str_to_seconds('16:00:00') - str_to_seconds('13:15:00'));
You must convert it to timestamp, then calculate the difference and format that difference to appear like a normal date.
Something like this:
function dateDifference($date_1 , $date_2 , $differenceFormat = '%a' )
{
$datetime1 = date_create($date_1);
$datetime2 = date_create($date_2);
$interval = date_diff($datetime1, $datetime2);
return $interval->format($differenceFormat);
}
Super short version
$secondsDiff = abs(strtotime("2014-02-11 04:04:26 AM") - strtotime("2014-02-11 05:36:56 AM"));
echo sprintf("%02d", floor($secondsDiff / 3600)) . ":" . sprintf("%02d", floor($secondsDiff / 60 %60)) . ":" . sprintf("%02d", ($secondsDiff % 60));
I'm trying to get remaining Days, hours and minutes to a certain date using php.
However i get a very strange output from my code which looks like this:
-16828 days and -11 hours and -21 minutes and -24 seconds
The future dates are stored in the mysql database in this format:
29/01/2016 7pm
So I went ahead and done this:
$Draw_time = "29/01/2016 7pm";
$date = $Draw_time;
$timestamp = strtotime($date);
$new_date = date('Y-m-d a',$timestamp );
$seconds = strtotime($new_date) - 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";
But when i run this code, I get the above strange output!
I understand that this could be because of a number reasons but the only thing i could think of is the fact that I am using a in my format?
Could someone please advise on this issue?
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");
Try this
<?php
$Draw_time = str_replace('/', '-', "29/01/2016 7pm");
$now = new DateTime();
$futureDate = new DateTime($Draw_time);
$interval = $futureDate->diff($now);
echo $interval->format("%a days %h hours %i minutes %s seconds");
?>
Try this.
$draw_time = "2016/01/29 7pm";
$date_time = explode(" ", $draw_time);// make separate date and time in array
$date = strtotime($date_time[0]); // convert your date(2016/01/29) into php time
$time = strtotime($date_time[1]); // convert your time(7pm) into php time
$date = $date + $time; // make total time to count
$new_Date = $date - (time()); // convert into difference from current time
$day = $new_Date % 86400;
$hrs = $new_Date % 3600;
$min = $new_Date % 60;
echo "Day= ".(date("d",$day));
echo " Hours= ".(date("h",$hrs));
echo " Minutes= ".(date("i",$min));
The future time is :2012-05-26 00:00:00
supposed there are three variable: $hour $minute $second
now, i want to using the future time subtract now time. then give the left hour to $hour,give the left minute to $minute,give the left second to $second.
i am sorry i am new of php, now i get stucked how to do the math operation ? thank you
A very good resource for dates and time..
http://www.php.net/manual/en/function.time.php
-there are samples here doing something similar.
Check the date_diff function. There's the exact solution to what you're asking there.
And here's the page (DateInterval::format) documenting how you can format the output.
$now = date_create();
// use "now" and necessary DateTimeZone in the arguments
$otherDate = date_create('2020-04-13');
$interval = date_diff($now, $futureDate);
echo $interval->format('%a days');
The following are the math operations for the difference in hours,minutes and seconds
$future_datetime = '2012-05-26 00:00:00';
$future = strtotime($future_datetime); //future datetime in seconds
$now_datetime = date('Y-m-d H:i:s');
$now = date('U'); //now datetime in seconds
//The math for calculating the difference in hours, minutes and seconds
$difference = $future - $now;
$second = 1;
$minute = 60 * $second;
$hour = 60 * $minute;
$difference_hours = floor($difference/$hour);
$remainder = $difference - ($difference_hours * $hour);
$difference_minutes = floor($remainder/$minute);
$remainder = $remainder - ($difference_minutes * $minute);
$difference_seconds = $remainder;
echo "The difference between $future_datetime and $now_datetime is $difference_hours hours, $difference_minutes minutes and $difference_seconds seconds";
I use this function to set a time date("Y-m-d H:i:s", strtotime("+2 minutes"). Now I want to compare that value with the current time to find the amount of seconds it's left.
For example compare: $next_action = 2011-01-16 18:03:00 and $now = 2011-01-16 18:01:23. To find the amount of seconds.
strtotime can convert mysql timestamps to unix timestamps. so you just convert both of them to UNIX timestamps and subtract one from other, and you'll get the difference in seconds.
$next_action = "2011-01-16 18:03:00";
$now = "2011-01-16 18:01:23";
echo strtotime($next_action)-strtotime($now);
Why did you convert them to "Y-m-d H:i:s" in the first place? Unix timestamps are much easier to work with.
$start_time = date("Y-m-d H:i:s", strtotime("+2 minutes"));
$time_diff = (time() - strtotime($start_time)); // difference in seconds
$seconds = $time_diff % 60;
$minutes = ($time_diff - $seconds) % (60 * 60);
$hours = ($time_diff - ($minutes * 60) - $seconds) / (24 * 60 * 60);
Untested, but it would probably go something like this.
I have two dates, formated like "Y-m-d H:i:s". I need to compare these two dates and figure out the hour difference.
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.
You can use DateTime class also -
$d1= new DateTime("06-08-2015 01:33:26pm"); // first date
$d2= new DateTime("06-07-2015 10:33:26am"); // second date
$interval= $d1->diff($d2); // get difference between two dates
echo ($interval->days * 24) + $interval->h; // convert days to hours and add hours from difference
As an addition to accepted answer I would like to remind that \DateTime::diff is available!
$f = 'Y-m-d H:i:s';
$d1 = \DateTime::createFromFormat($date1, $f);
$d2 = \DateTime::createFromFormat($date2, $f);
/**
* #var \DateInterval $diff
*/
$diff = $d2->diff($d1);
$hours = $diff->h + ($diff->days * 24); // + ($diff->m > 30 ? 1 : 0) to be more precise
\DateInterval documentation.
$seconds = strtotime($date2) - strtotime($date1);
$hours = $seconds / 60 / 60;
You can try this :
$time1 = new DateTime('06:56:58');
$time2 = new DateTime('15:35:00');
$time_diff = $time1->diff($time2);
echo $time_diff->h.' hours';
echo $time_diff->i.' minutes';
echo $time_diff->s.' seconds';
Output:
8 hours 38 minutes 2 seconds
The problem is that using these values the result is 167 and it should be 168:
$date1 = "2014-03-07 05:49:23";
$date2 = "2014-03-14 05:49:23";
$seconds = strtotime($date2) - strtotime($date1);
$hours = $seconds / 60 / 60;
$date1 = date_create('2016-12-12 09:00:00');
$date2 = date_create('2016-12-12 11:00:00');
$diff = date_diff($date1,$date2);
$hour = $diff->h;
This is because of day time saving.
Daylight Saving Time (United States) 2014 began at 2:00 AM on
Sunday, March 9.
You lose one hour during the period from $date1 = "2014-03-07 05:49:23" to
$date2 = "2014-03-14 05:49:23";
You can try this:
$dayinpass = "2016-09-23 20:09:12";
$today = time();
$dayinpass= strtotime($dayinpass);
echo round(abs($today-$dayinpass)/60/60);
You can use strtotime() to parse your strings and do the difference between the two of them.
Resources :
php.net - strtotime()