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));
Related
I have a PHP and MySQL code that should calculate the hours minutes and days of difference between two date and hours. It works well, just adding 20 hours and 20 minutes more than normal. And I remove the DATE part and put the date and time manually, it works fine.
I don't understand what happens.
$fecha = $row["fecha"];
$data= $row["hora"];
$start = strtotime("$fecha $hora");
$currentDate = date("Y-m-d");
$currentTime = date("H:i:s");
$currentDate = date("Y-m-d H:i:s", strtotime($currentDate .$currentTime));
$end = strtotime("$currentDate");
$totaltime = ($end - $start) ;
$hours = intval($totaltime / 3600);
$seconds_remain = ($totaltime - ($hours * 3600));
$minutes = intval($seconds_remain / 60);
$seconds = ($seconds_remain - ($minutes * 60));
$statusfichaje= $row["status"];
if ($statusfichaje == Start){echo '<td>Trabajando'.$hours.':'.$minutes.':'.$seconds.' </td>';}else{echo '<td>'. $row["status"] .'</td>';}
Edit
start 2019-12-29 21:27:50 . end 2019-12-31 0:51:50 = 47:51:16
As you can see it calculates badly.
A simple example like this would do the job :
$mydatetime = new DateTime();
$datefromdb = new DateTime('2018-03-05 10:10:00');
$interval = $mydatetime->diff($datefromdb);
$date_count = $interval->format('%y years %m months %a days %h hours %i minutes %s seconds');
echo $date_count;
This is your code it should work
$fecha = $row["fecha"];
$data= $row["hora"];
$start = strtotime("$fecha $data");
$currentDate = date("Y-m-d");
$currentTime = date("H:i:s");
$currentDate = date("Y-m-d H:i:s", strtotime($currentDate .$currentTime));
$end = strtotime("$currentDate");
$totaltime = ($end - $start) ;
$hours = intval($totaltime / 3600);
$seconds_remain = ($totaltime - ($hours * 3600));
$minutes = intval($seconds_remain / 60);
$seconds = ($seconds_remain - ($minutes * 60));
$statusfichaje= $row["status"];
if ($statusfichaje == $start){echo '<td>Trabajando'.$hours.':'.$minutes.':'.$seconds.' </td>';}else{echo '<td>'. $row["status"] .'</td>';}
problem was in the string.
$data= $row["hora"];
and I use this
$start = strtotime("$fecha $hora");
And don't take the hours and calculate only for days.
Thank you
I need to add a time stored in strong (format: hh:mm) to a date timestamp which before have no hours information (e.g. 2018-06-12 00:00:00). How to do it?
I would like to do this:
$startTime = "11:45";
$startDate = 1528754400; // 2018-06-12 00:00:00
$startDate = $startDate + $startTime; //2018-06-12 11:45:00
You can do some simple math and get there. Multiple minutes by 60 and hours by 3600 and you'll get the number of seconds the hours/minutes take up.
$startTime = "11:45";
$startDate = 1528754400;
$time = explode(':', $startTime);
$minutes = $time[1] * 60;
$hours = $time[0] * 3600;
Then just piece it back together. (the timestamp is the number of seconds since 1970 so just adding to it should be fine)
$startDate + $minutes + $hours
https://3v4l.org/U2jHL
An alternative approach would be using the datetime class.
$startTime = "11:45";
list($hours, $minutes) = explode(':', $startTime);
$date = new DateTime(date('Y-m-d', 1528754400));
$date->add(new DateInterval('PT' . $hours .'H' . $minutes . 'M'));
https://3v4l.org/D51XB
Just came across with this using strtotime:
$startTime = "11:45";
$startDate = 1528754400; // 2018-06-12 00:00:00
$startTimeTmp = explode(":",$startTime);
$finalTimestamp = strtotime("+".$startTimeTmp[0]." hours ".$startTimeTmp[1]." minutes", $startDate);
So far I've got this script that counts down the days and hours, but how can I make it also do minutes and seconds?
$remaining = strtotime($ActiveListing['ListingExpires']) - time();
$days_remaining = floor($remaining / 86400);
$hours_remaining = floor(($remaining % 86400) / 3600);
I thought of a solution with date():
$remaining = strtotime($ActiveListing['ListingExpires']) - time();
list($months, $days, $hours, $minutes, $seconds) = explode(" ",date("n j H i s",$remaining));
$months--;$days--;
echo "$months months - $days days - $hours hours - $minutes minutes - $seconds seconds left";
$remaining = strtotime($ActiveListing['ListingExpires']) - time();
$days_remaining = floor($remaining/60/60/24);
$hours_remaining = floor(($remaining-($days_remaining*60*60*24))/60/60);
$minutes_remaining = floor(($remaining-($days_remaining*60*60*24)-($hours_remaining*60*60))/60);
$seconds_remaining = floor(($remaining-($days_remaining*60*60*24)-($hours_remaining*60*60))-($minutes_remaining*60));
$edate = '2027-07-06 07:01:53';
$datestr = $edate;//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));
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
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()