I'm having problem when getting the exact number of days. Given I have date/time which consider hours in counting number of days below the code give me zero days
$fisrstDate = new DateTime("2018-03-07 04:46:00");
$secondDate = new DateTime("2018-03-07 11:10:00");
$days=$fisrstDate->diff($secondDate)->days;
another example is this it should give me 2 days but shows only 1 days my idea is when 24 hours exceed I want to add another 1 days so that it would give me an output of 2 days
$fisrstDate = new DateTime("2018-03-07 04:46:00");
$secondDate = new DateTime("2018-03-08 05:00:00");
$days=$fisrstDate->diff($secondDate)->days;
You can use strtotime to get the exact seconds between two time stamps and then convert it to days followed by ceil to make it work. Eg:
$fisrstDate = strtotime("2018-03-07 04:46:00");
$secondDate = strtotime("2018-03-07 11:10:00");
$days = abs(ceil((abs($fisrstDate - $secondDate)/ (60 * 60 * 24)) - (1 / 24)));
echo $days;
Isn't it just date2 - date1 + 1?
Stuck on this question for college!
subtract one time from another. The times are in the format “HH:MM:SS:FF” where:
HH = hours – there are 24 hours in a day
MM = minutes – there are 60 minutes in a hour
SS = seconds – there are 60 seconds in a minute
FF = frames – there are 25 frames in a second
The time is always 11 characters long and each element will always be 2 characters – padded with zeros if required.
subtract $y from $x
'$x = “03:14:59:20”;
$y = “01:16:01:02”;
I have done this code
<?php
$now = new DateTime(); // current date/time
$now = new DateTime("03:14:59:20");
$ref = new DateTime("01:16:01:02");
$diff = $now->diff($ref);
printf('%d hours, %d minutes %d seconds %d frames', $diff->h, $diff->i, $diff->s, $diff->f);
but the frames part does not work
Thanks in advance
I have a starttime and endtime, which always start or end on exact a hour :00 or :15, :30, :45.
For example:
00:30 21-05-2012 // startdate and time
18:15 22-05-2012 // enddate and time (+1 day)
I have to charge an extra % if the time falls into evening or night hours.
So I have 2 arrays, one for evening hours and one for night hours which looks like this.
$eveningHours = array(
'18:00',
'18:15',
'18:30',
// etcetera
'00:00'
);
The above array basically covers the time from 18:00 - 00:00, since these hours are falling into a evening hours.
Same goes for night hours, they cover the time from 00:00 - 07:00.
Since the start time and end time always land on exact :00 or :15, :30, :45.. I run a loop between the start datetime and the end datetime with an interval of 15 minutes and then on each interval I check if the current intervaltime is night/evening hour and then Ii add +15 to a evening / night hour counter and at the end I divide this by 60 to get the total hours.
But somehow it always has 15 minutes to much or 15 minutes to little... I've tried almost everything and can't get it to work properly..
So for example the start time is 00:30 it already adds 15 minutes to the first interval:
00:30 (counter: +15)
00:45 (counter: +15)
Which totals 30 minutes, but if you work from 00:30 to 00:45 it should offcourse only be 15 minutes.. So that is the place where it goes wrong I think. I just need a way to fix it.. I've already tried so many things by starting the counter with -15 minutes, or check if the start or end time is between a hour range and then set the counter to -15 or + 15 minutes.. but this also goes wrong.
EDIT this is the basic of the loop:
$start_time = mktime($_POST['worked_start_hour'], $_POST['worked_start_min'], 0, date("m"), date("d", strtotime("+1 days")), date("Y")); // +1 day for testing purpose
$end_time = mktime($_POST['worked_end_hour'], $_POST['worked_end_min'], 0, date("m"), date("d", strtotime("+2 days")), date("Y")); // +2 days for testing purpose
$date['start_h_i'] = date("Y-m-d H:i", $start_time); # Start date: yyyy-mm-dd hours:minutes
$date['end_h_i'] = date("Y-m-d H:i", $end_time); # End date: yyyy-mm-dd hours:minutes
$counter['eveningMinutes'] = 0;
$counter['nightMinutes'] = 0;
$eveningHours = array(
'18:00',
'18:15',
'18:30',
'18:45',
'19:00',
'19:15',
'19:30',
'19:45',
'20:00',
'20:15',
'20:30',
'20:45',
'21:00',
'21:15',
'21:30',
'21:45',
'22:00',
'22:15',
'22:30',
'22:45',
'23:00',
'23:15',
'23:30',
'23:45',
'00:00'
);
$nightHours = array(
'00:00',
'00:15',
'00:30',
'00:45',
'01:00',
'01:15',
'01:30',
'01:45',
'02:00',
'02:15',
'02:30',
'02:45',
'03:00',
'03:15',
'03:30',
'03:45',
'04:00',
'04:15',
'04:30',
'04:45',
'05:00',
'05:15',
'05:30',
'05:45',
'06:00',
'06:15',
'06:30',
'06:45',
'07:00'
);
while (strtotime($date['start_h_i']) <= strtotime($date['end_h_i'])) {
if (in_array(date("H:i", strtotime($date['start_h_i'])), $eveningHours)) {
$counter['eveningMinutes'] = $counter['eveningMinutes'] + 15;
}
if (in_array(date("H:i", strtotime($date['start_h_i'])), $nightHours)) {
$counter['nightMinutes'] = $counter['nightMinutes'] + 15;
}
$date['start_h_i'] = date ("Y-m-d H:i", strtotime("+15 minutes", strtotime($date['start_h_i']))); // 15 minutes interval
} // end while
I hope someone can shine some light here, thanks!
Anyone?
Change this line:
while (strtotime($date['start_h_i']) <= strtotime($date['end_h_i'])) {
to this:
while (strtotime($date['start_h_i']) < strtotime($date['end_h_i'])) {
The problem you're having is that you're updating the hours when you've already finished counting up time. So to use your own example of 00:30 - 00:45:
In loop first time: 00:30 <= 00:45, so add 15 minutes
In loop second time: 00:45 <= 00:45, so add 15 minutes
By changing the <= to a < you remove that second operation: 00:45 !< 00:45 so add nothing.
I have two times like 10:00 am and 7:00 pm.
And from this I want to get the total hours. As from that time I have to get the 9 hours.
How will I do this?
I have explode it with the : but it then subtract 7 from 10 and return the result 3 which is incorrect because it should return 9.
<?php echo strtotime('7:00 pm')-strtotime('10:00 am');?>
Get the timestamp difference
EDIT
echo (strtotime('07:00 pm')-strtotime('10:00 am'))/(60*60); //displays 9
60*60 = 1 Hrs
EDIT
$fromTime = '3:00 pm';
$toTime = '12:00 am';
$timediff = (strtotime($toTime)-strtotime($fromTime))/(60*60);
echo $timediff >= 0 ? $timediff : (24 + $timediff);
You can use the strtotime function of PHP to convert this to unix time and so you can do the further calculation with it.
I feel this will help you out more effectively
<?php echo date('H:i:s',strtotime('7:00 pm')-strtotime('10:00 am'));?>
I assume you can get hold of the am or pm information! With this information you can make an if() to make it correct!
If() it is PM, add 12 hours, if it is not PM, don't do anything.
Do this with the times you're comparing:
if (pm==1){
time+=12;
}
so 10:00 am = false it will be 10:00
and 7:00 pm = true it will be 19:00
19:00 - 10:00 = 9 hours = win.
Use DateTime interface, Its simple
$day1= new DateTime("today 01:33:26am");
$dayd2= new DateTime("today 10:40:36pm"); //Output: Hours: 21
$interval= $day1->diff($day2);
$h = ($interval->days * 24) + $interval->h;
echo "Hours: ".$h
Here
h = number of hours.
$interval->days means how many days get from difference if we get one(1) day then it's add 1*24(hours) = 24+21 = 46 hour's but here day1 and day2 contains today so, that's means 0*24 = 0 + 21 = 21
Finally Output: 21 hour's
Editing the question.
I have SQL like this:
`table1`.`DateField` >= DATE_SUB(NOW(), INTERVAL {$days} DAY
Now 24 hours make a whole day. However, what if I want to do the query for the last 3 hours or so?
My table1.DateField is in the format of 2010-03-10 10:05:50.
Original post:
If I have this
1 hour
2 hours
3 hours
..
24 hours
How would I change it to days?
Thanks.
$hours = 80;
$hid = 24; // Hours in a day - could be 24, 8, etc
$days = round($hours/$hid);
if( $days < 0 )
{
echo "$hours hours";
}
else
{
echo "$days days";
}
This assumes you want the hours if it's less than 1 day. If not just remove the switch.
MySQL not only knows DAY as a unit for an interval but also HOUR, MINUTE, ....
see http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add
$x = 32;
$sql = "SELECT
x,y,z
FROM
foo
WHERE
`table1`.`DateField` >= NOW() - INTERVAL $x HOUR
";
As simple as:
if you want to convert the total of those hour to day:
Just sum the total of hours and that total must be divided by 24
(1 + 2 + 3 + 5) / 24
If you want to convert all of those hours to days:
Just divide by 24 every hours in your list
(1/24) (2/24) (3/24) (5/24)