From my user table I retrieve an integer (e.g. 60) which is supposed to be the maximum of minutes that the user may use for his total break time.
When the user enters and exits his break, it's reorded in another table like this:
$timestamp = date("Y-m-d H:i:s");
$sql = "INSERT INTO user_log (username, in_pause, action_time)
VALUES ('$username', 1, '$timestamp')";
Where I write 1 for when he enters and 0 for when he exits.
I calculate how long the user's break lasted from those two recorded timestamps like this (after my sql query i put the timestamps into $zeiten[]):
$pause_start = new DateTime($zeiten[1]);
$pause_ende = new DateTime($zeiten[0]);
$pause_diff = $pause_start->diff($pause_ende);
This works fine so far. But I can't seem to find a way to manipulate the integer (here 60) from my user table so that I can subtract my $pause_diff from it. I want to be able to calculate the break time that the user has left.
Thanks!
Try this code:
<?php
$max_minutes = (int) 60;
$zeiten[1] = '2000-01-01 10:00:00';
$zeiten[0] = '2000-01-01 10:10:00';
$pause_start = new DateTime($zeiten[1]);
$pause_ende = new DateTime($zeiten[0]);
$pause_diff = $pause_start->diff($pause_ende);
//$pause_diff->format('Y-m-d H:i:s');
$pause_diff_minutes = $pause_diff->format('%i');
//var_dump((int) $pause_diff_minutes);
echo 'the differnece is: ' . ($max_minutes - (int) $pause_diff_minutes) . ' minutes';
The result is:
the differnece is: 50 minutes
You can play with the code here
Updated code showing how you could subtract also seconds from the 60 minutes:
<?php
$max_minutes = (int) 60;
$max_minutes_seconds = $max_minutes * 60; // max_minutes to seconds
$zeiten[1] = '2000-01-01 10:00:00';
$zeiten[0] = '2000-01-01 10:10:05';
$pause_start = new DateTime($zeiten[1]);
$pause_ende = new DateTime($zeiten[0]);
$pause_diff = $pause_start->diff($pause_ende);
$pause_diff_minutes = $pause_diff->format('%i');
$pause_diff_seconds = $pause_diff->format('%s');
// sum total diff in seconds
$total_pause_seconds = ((int) $pause_diff_minutes * 60) + $pause_diff_seconds;
$total_diff_seconds = ($max_minutes_seconds - $total_pause_seconds);
echo 'the differnece is: ' . $total_diff_seconds . ' seconds' . "\n";
echo 'which is: ' . floor($total_diff_seconds / 60) . ' minutes and ' . $total_diff_seconds % 60 . ' seconds';
The result is:
the differnece is: 2995 seconds
which is: 49 minutes and 55 seconds
play with it here
I have 2 time strings for example OPEN = 11:00 and closed is 02:45
now the 02:00 is the next day so i to the following:
//open = string 11:00
//closed= string 02:45
$open = \DateTime::createFromFormat('H:i', $open);
$closed = \DateTime::createFromFormat('H:i', $closed);
if ($open > $closed) $closed->modify('+1 day');
now i have 2 proper datetime formats. Now i want to set a time interval of 30 minutes from the open time to the close time. How can i do this? i have read i can add like this
->add(new DateInterval('PT30M') b
ut it will add till the end of the day.. but in this case its open till the next day, so i want it to populate till 2:45AM
how can i do this?
You where on the right track. Here is how to do it using DateInterval('PT30M').
$strOpen = '11:00';
$strClose = '02:45';
$open = DateTime::createFromFormat('H:i', $strOpen);
$closed = DateTime::createFromFormat('H:i', $strClose);
if ($open > $closed) $closed->modify('+1 day');
// I display not only the time, but the day as well to show that it is
// incrementing to the next day.
echo 'open: ' . $open->format('D H:i') . "<br />\n";
while ($open < $closed) {
$open->add(new DateInterval('PT30M'));
// because incrementing on the half hour and our finish is on the 15 min,
// the last is $open < $close in the while statement will be true but
// this loop will generate a time after $closed so we do another check
// now to eliminate that issue
if ($open < $closed) {
echo '+30min: ' . $open->format('D H:i') . "<br />\n";
}
}
echo 'closed: ' . $closed->format('D H:i') . "<br />\n";
Supplementry Question to timestamp - php incrementing time stamp error
Whilst accepting that class DateTime may provide a resolution to my original query there remains the unexplained variance in the timestamps. I really would like to understand this variance, whether there are other such timestamp "adjustments" and how they arise.
Please consider the following:
/*
* test time stamp variances
*/
$time_Stamp_1 = mktime(0,0,0,10,15,2012);echo "15/10/12: " . $time_Stamp_1;
$time_Stamp_2 = mktime(0,0,0,10,16,2012);echo "<br/>16/10/12: " . $time_Stamp_2 . "increment= " . ($time_Stamp_2 - $time_Stamp_1);
$time_Stamp_3 = mktime(0,0,0,10,17,2012);echo "<br/>17/10/12: " . $time_Stamp_3 . "increment= " . ($time_Stamp_3 - $time_Stamp_2);
$time_Stamp_4 = mktime(0,0,0,10,18,2012);echo "<br/>18/10/12: " . $time_Stamp_4 . "increment= " . ($time_Stamp_4 - $time_Stamp_3);
$time_Stamp_5 = mktime(0,0,0,10,19,2012);echo "<br/>19/10/12: " . $time_Stamp_5 . "increment= " . ($time_Stamp_5 - $time_Stamp_4);
$time_Stamp_6 = mktime(0,0,0,10,20,2012);echo "<br/>20/10/12: " . $time_Stamp_6 . "increment= " . ($time_Stamp_6 - $time_Stamp_5);
$time_Stamp_7 = mktime(0,0,0,10,21,2012);echo "<br/>21/10/12: " . $time_Stamp_7 . "increment= " . ($time_Stamp_7 - $time_Stamp_6);
$time_Stamp_8 = mktime(0,0,0,10,22,2012);echo "<br/>22/10/12: " . $time_Stamp_8 . "increment= " . ($time_Stamp_8 - $time_Stamp_7);
$time_Stamp_9 = mktime(0,0,0,10,23,2012);echo "<br/>23/10/12: " . $time_Stamp_9 . "increment= " . ($time_Stamp_9 - $time_Stamp_8);
$time_Stamp_10 = mktime(0,0,0,10,24,2012);echo "<br/>24/10/12: " . $time_Stamp_10 . "increment= " . ($time_Stamp_10 - $time_Stamp_9);
$time_Stamp_11 = mktime(0,0,0,10,25,2012);echo "<br/>25/10/12: " . $time_Stamp_11 . "increment= " . ($time_Stamp_11 - $time_Stamp_10);
$time_Stamp_12 = mktime(0,0,0,10,26,2012);echo "<br/>26/10/12: " . $time_Stamp_12 . "increment= " . ($time_Stamp_12 - $time_Stamp_11);
$time_Stamp_13 = mktime(0,0,0,10,27,2012);echo "<br/>27/10/12: " . $time_Stamp_13 . "increment= " . ($time_Stamp_13 - $time_Stamp_12);
$time_Stamp_14 = mktime(0,0,0,10,28,2012);echo "<br/>28/10/12: " . $time_Stamp_14 . "increment= " . ($time_Stamp_14 - $time_Stamp_13);
$time_Stamp_15 = mktime(0,0,0,10,29,2012);echo "<br/>29/10/12: " . $time_Stamp_15 . "increment= " . ($time_Stamp_15 - $time_Stamp_14);
Reports:
15/10/12: 1350255600
16/10/12: 1350342000increment= 86400
17/10/12: 1350428400increment= 86400
18/10/12: 1350514800increment= 86400
19/10/12: 1350601200increment= 86400
20/10/12: 1350687600increment= 86400
21/10/12: 1350774000increment= 86400
22/10/12: 1350860400increment= 86400
23/10/12: 1350946800increment= 86400
24/10/12: 1351033200increment= 86400
25/10/12: 1351119600increment= 86400
26/10/12: 1351206000increment= 86400
27/10/12: 1351292400increment= 86400
28/10/12: 1351378800increment= 86400
29/10/12: 1351468800increment= 90000
Hence:
> 15/10/2012 1350255600 + 604800 does increment 1 week to 22/10/2012 ..
> 22/10/2012 1350860400 + 604800 does not increment 1 week to 29/10/2012
> because although this results in 1351465200 which should be 29/10/2012
> you can see from the above that it resolves to 28/10/2012 because for
> some unexplained reason an extra hour 3600sec has been added to the
> time stamp for 29/10/2012.
I want to know because from my reading of the documentation mktime should create a timestamp just as valid as say strtotime or DateTime.
Indeed using class DateTime method getTimestamp
28/10/2012 = 1351378800
29/10/2012 = 1351468800 an increment of 90000
So once again an hour has been added implying that I am correct in this assumption.
Obviously the class can deal with this. BUT no where up until this point has there been any mention of the fact that incrementing a timestamp however it is generated could result in an issue... thus making the use of the DateTime class or another approach mandatory to avoid issues such as have been encountered.
If I have to convert the code you use the class so be it. But I would like to know why this is necessary.
Due to a daylight savings shift, 28/10/12 has an extra hour in your timezone. There are 25 hours between midnight 28/10 and midnight 29/10.
You will also find a day with 23 hours in spring.
If this is not what you expect, change the timezone to something that has no DST. UTC is one option:
php > echo mktime(0,0,0,10,29,2012) - mktime(0,0,0,10,28,2012);
90000
php > ini_set('date.timezone', 'UTC');
php > echo mktime(0,0,0,10,29,2012) - mktime(0,0,0,10,28,2012);
86400
I'm going to take a stab at this. October 29th is exactly 1 week before daylight savings time (unless you are in a country with a different daylight savings time). But falling back 1 hour would cause a day to have 1 more hour.
I am trying to display some content only for some time or some days
I have start time, start date, end time and end date in database so that when the data is fetched from database this time and dates should be checked and display the content accordingly
My code that i have tried
$startDateTime = strtotime($result['StartDate']." ".$result['StartTime']);
$endDateTime = strtotime($result['EndDate']." ".$result['EndTime']);
$now = time();
echo "START : ".$startDateTime;
echo "<br/>END : ".$endDateTime;
echo "<br/>CURRENT : ".$now;
if($now >= $startDateTime && $now <= $endDateTime){
echo $result['content'];
}
But its not working its displaying the content every time
Please Help Me
Thanks in advance
output a list of your $result['StartDate']." ".$result['StartTime'] I think you'll find that it might be leaving off leading 0's or something which would give it trouble casting it as a time.
Rather than using the time() function - try creating '$now' as strtotime("now").
Also, as a debugging step, it probably would have helped you to echo the comparisons to see if they evaluated the way that you expected.
Actually - I just tried your code exactly as it was...Except that I had hard coded values for the starting and ending dates and times. And it worked, so switching to use strtotime("now") will probably not make a difference.
What are the values that were printed out? You didn't tell us.
So VoronoiPotato is probably right...The values you are storing for StartDate/StartTime and EndDate/EndTime must have something wrong with them.
What is the output of your program if you change it like this (to add more debugging):
$startDateTime = $result['StartDate'] . " " . $result['StartTime'];
$endDateTime = $result['EndDate'] . " " . $result['EndTime'];
$now = time();
echo "START : " . $startDateTime."<br />\n";
echo "END : " . $endDateTime . "<br />\n";
$startDateTime = strtotime($startDateTime);
$endDateTime = strtotime($endDateTime);
echo "Start as time: " . $startDateTime . "<br />\n";
echo "End as time: " . $endDateTime . "<br />\n";
echo "CURRENT : " . $now . "\n";
if (($now >= $startDateTime) && ($now <= $endDateTime)) {
echo $result['content'];
}
i've to display the differences between a column(reg_time) in mysql and the present time so that the final output (after some php to display in minutes or seconds or hours or days)
be:
Registered : 8 hours ago.
is there any function to do that?
thanks
//End date is current date
//$start_date, lets say sep 05 82 : 00:00:00
$seconds_dif = mktime(date("G"),date("i"),date("s"),date("m"),date("d"),date("Y")) - mktime('00','00','00','09','05','1982');
//Difference in seconds is $seconds_dif
//Now only math calculation to figure out months/years..
echo '<br />Years Difference = '. floor($seconds_dif/365/60/60/24);
echo '<br />Months Difference = '. floor($seconds_dif/60/60/24/7/4);
echo '<br />Weeks Difference = '. floor($seconds_dif/60/60/24/7);
echo '<br />Days Difference = '. floor($seconds_dif/60/60/24);
echo '<br />Hours Difference = '. floor($seconds_dif/60/60);
echo '<br />Minutes Difference = '. floor($seconds_dif/60);
Found here: http://www.webhostingtalk.com/showthread.php?t=453668
You need to edit the 3rd line of code for your data format:
mktime('00','00','00','09','05','1982')
Also you need to wrap this code into a function for easy using it:
/**
* Return string with date time difference between two arguments
* #param $start_date integer Start date in unix time (seconds from January 1 1970), you can use strtotime('24 November 2003 13:45:54'), for example
* #param $end_date integer [optional] End date in unix time, by default it equals now
* #return string
*/
function getDateTimeDifference($start_date, $end_date = time()) {
$end_date = mktime(date("G", $end_date),date("i", $end_date),date("s", $end_date),date("m", $end_date),date("d", $end_date),date("Y", $end_date));
$difference = $end_date - mktime(date("G", $start_date),date("i", $start_date),date("s", $start_date),date("m", $start_date),date("d", $start_date),date("Y", $start_date));
$years = floor($seconds_dif/365/60/60/24);
$months = floor($seconds_dif/60/60/24/7/4);
$weeks = floor($seconds_dif/60/60/24/7);
$days = floor($seconds_dif/60/60/24);
$hours = floor($seconds_dif/60/60);
$minutes = floor($seconds_dif/60);
$ret = 'Difference: ';
if (!empty($years))
$ret .= $years . ' years, ';
if (!empty($months))
$ret .= $months . ' months, ';
if (!empty($weeks))
$ret .= $weeks . ' weeks, ';
if (!empty($days))
$ret .= $days . ' days, ';
if (!empty($hours))
$ret .= $hours . ' hours, ';
if (!empty($minutes))
$ret .= $minutes . 'minutes';
return $ret;
}
Take a look at the MySQL functions timediff() (4.1.1+) and timestampdiff() (5.0+).
And the php method DateTime::diff() (5.3+)
e.g.
SELECT Now(), TIMESTAMPDIFF(HOUR, Now(), '2009-12-12 06:15:34')
just returned
"2009-12-12 12:34:00"; "-6"
on my machine. And
$dt = new DateTime('2008-03-18 06:15:34');
echo $dt->diff(new DateTime())->format('%y %d %h');
(currently) prints
1 25 6