Cakephp: Time, diff not working as expected - php

I am building a task manager application where the user can start/stop a task. And I want to know when the user started it, when stopped it and how long task was running.
In the appcontroller.php I have set this timezone:
date_default_timezone_set("Europe/Athens");
But in taskscontroller.php when I save the time that the task has been started:
$now = FrozenTime::now();
$task->date_start = $now;
It is saved 2 hours ahead in the database (the field in the database is timestamp type).So when I click start at 9:00:00 I see that is saved as 11:00:00 in the database. Same when I click stop:
$now = FrozenTime::now();
$task->date_end = $now;
Again it is saved 2 hours ahead. But in my application when user clicks stop I want to calculate the time that the task was running. So I am calculating like this:
$task->total_minutes = ($now->diff($task->date_start))->format('%i'); //minutes
But even though date_start is : 2019-03-18 11:43:47
and date_end is : 2019-03-18 11:45:33
I get total_minutes: 58 which is obviously wrong... Why I get all these faulty behaviors?

Related

Some questions about EDT and time difference

Introduction to my website
My website is for visitors in Korea(AKA Republic of Korea).
And the server for My website is in the United States of America.
And PHPMyAdmin displays EDT when it runs a query SELECT ## system_time_zone.
Structure of my website
When I first uploaded my website to this server in October this year, I checked the DB time.
And it seemed that there was a time difference of 13 hours with Korea. So I added 3600 * 13 seconds to DB time(without setting timezone) as follows.
const Offset = 3600 * 13;
$SelectNow = $PDO->prepare('SELECT DATE_ADD(NOW(), INTERVAL '.Offset.' SECOND)');
$SelectNow->execute() or exit;
$DbNow = $SelectNow->fetchColumn();
My website takes $DbNow as above and uses it in various situations.
For example, in the posting situation, enter $DbNow in the datetime field of the INSERT INTO query as follows:
$WriteNote = $PDO->prepare('INSERT INTO table_note(my_datetime, my_contents) VALUES("'.$DbNow.'", :my_contents)');
$WriteNote->bindValue(':my_contents', $my_contents, PDO::PARAM_STR);
$WriteNote->execute();
The problem situation
One day in November of this year, when I wrote a post and checked the date field(my_datetime) of the post, I got an additional time difference of one hour with Korea.
Apparently, at the end of October, I corrected the time difference of 3600 * 13. And then I confirmed that it matches the Korean time. However, in November, There is a time difference of one hour!
Guess the cause
It seems that US summer time is being applied to the DB server of my website. Did I guess right?
My question
1) How can I solve this time difference fundamentally?
Is it correct to convert DB time to KST?
Or is it the correct way to convert to UTC and then added 3600 * x to UTC?
2) Even though the problem is resolved, some of the existing data in my DB has a time difference of one hour with Korean time.
What query do I use if I want to select the data with a time difference?
And how much more or subtract it from the data to get rid of the 1 hour time difference?
Use UTC to store time in Database.
change your queries to insert with UTC datetimes.
Use external libraries to convert UTC to respective timezones.
(below are the my personal recommendation.)
There may be best of it.
PHP : Carbon
Javascript : Moment, moment timezone.
No, it takes timezone of Database server resides in.
little manual verification, or create a job to change all dates in UTC.
Edit:
http://carbon.nesbot.com/docs/
I mean you can create a script and run with cron job.

Time calculation activity in Php

I am working on a web application, in which a web user activity is to be block form half hour before of given time.
I need to calculate time half hour before of given time.
Means if a date and time give like 08/04/2015 16:00:00
now need to calculate time half hour before of the given time i.e. 08/04/2015 15:30:00 during this a web activity is blocked for end user.
Please give me suggestion and sample code in PHP.
you can take this for your refrence,
but you need to modify the code as per your requirements
$start_time = strtotime("2008-12-13 10:42:00"); // get this time while user loggs in
$end_time = strtotime(date('Y-m-d H:i:s'));// this is dynamic time, it changes everytime when the page is reloaded
$difference_in_minutes = round(abs($end_time - $start_time) / 60,2); // this will return the diference between two times in minutes
if($difference_in_minutes >= 30)
{
// do you 30 minutes block stuff here
}
let me know if any further classification needed

php detecting if user is offline using setinterval?

Hi guys i'm creating an online/offline system using PHP.
When user is logged in sessions are set and user is considered online and I run a set interval function that logs time(); into my database every 10 seconds.
setInterval(function(){
//update time every 10 seconds
$.get("timeupdate.php");
}, 10000);
I need some direction please in my next stage when I have to detect when the user is offline and I am slightly confused.
Do I run an if else statement?
$time = time();
$time2 = $row['time_update'] -> last updated time in my database
if($time > $time2 + 20) {
echo "user is offline";
}
because of the 10 second setinterval if $time ( the current unix timestamp) is greater than the last updated unix timestamp then user is offline.
am I right? and how would I go about implementing this display offline file?
Why implement the logic in PHP when the data in your database? If you're using MySQL date times....
SELECT last_seen_seconds_ago
, IF($MAXINTERVAL*1.05>last_seen_seconds_ago, 1, 0) AS status
FROM (
SELECT UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(time_update)
AS last_seen_seconds_ago
FROM yourtable
WHERE user='$user') ilv
(allows a 5% variance)

Compare user's time zone with the website's office location time zone

I am working on a project where i need to SHOW the visitor of the website a message in the contacts area like:
Contact no: +91-99-3241-5285 [You can call us now]
The message is highlighted in the above line, now my question is, how to compare the user's time zone with the working hours of the company's office.
P.S. - Suppose the company is in INDIA, and its working hours are 9.00am to 5.00pm.
Any suggestions?
Use javascript to determine the users timezone offset:
http://www.w3schools.com/jsref/jsref_gettimezoneoffset.asp
var d = new Date()
var gmtHours = -d.getTimezoneOffset()/60;
document.write("The local time zone is: GMT " + gmtHours);
Then calculate e.g. the current time in INDIA (GMT + 5:30 if i'm not wrong) and show "you can call us now" if the resulting time is within the working hours.
var diff = gmtHours - 5.5; // -> difference between users timezone and india in hours
var indiaDate = new Date();
indiaDate.setTime(d.getTime() - (diff * 60 * 60 * 1000));
document.write("<br />Current time in India: " + indiaDate.toString());
See the working demo at: http://jsfiddle.net/roberkules/RLsw9/
The replies immediately below your question are correct. There should be no need to know the client's time zone to calculate whether or not the current time is within hours of operation. It doesn't really matter whether it's currently 8:00am US Pacific, 11:00am US Eastern, or 11:00pm in Manila, Philippines. The important thing is what time it is relative to the business hours, not relative to the client.
Typically on all web servers I run, I set the server time to UTC. When people enter their hours, either have them enter it in GMT or, if you want them to be able to enter the hours in their local time, have a time zone list or combo box on the page. You can use PHP's DateTimeZone::listAbbreviations() function to help.
So, for example, say someone enters that their hours are from 9:00am to 5:00pm in the Asia/Calcutta time zone. When you're using the time for calculations, do something like this, assuming you have rows named something like hrs_start, hrs_end, and hrs_tz:
$hrs_tz = new DateTimeZone($row['hrs_tz']);
$hrs_start = new DateTime($row['hrs_start'], $hrs_tz);
$hrs_end = new DateTime($row['hrs_end'], $hrs_tz);
Now you can use these hours to calculate whether or not the hours are in or out of their business hours.

How to ensure that one time is always less than another

I've got two times that need to be stored into a database. The time format is hh:mm:ss with NO DATE. These times can be changed by the users on the system. One is a time on and the other is a time off. The time off should always be greater than the time on within a 24 hour cycle.
Here is the part that I'm having trouble with. I don't want to limit the user to selecting times before midnight to keep everything in the same "daily" cycle so I'd like to be able to logically determine if the users' times are simply within a 24 hour time period and then test that the on time is always less.
Can someone help me work through this? There are so many time and date functions that I really don't know which one(s) I need to do this; plus, I'm unclear on how I should test for this.
I'm starting to think that there is no way to test for this without having a date included. Just the times is not enough.
The time is always within a 24 hour cycle, so if the user puts 01:00/03:00 he's on for 2 hours
If he writes 03:00/01:00 he's on for 22 hours.
I dont see the problem.
The OP wrote in a comment:
The user can opt to get a report
delivered in a window of time. The
user may opt to have their reports
delivered in a window from 23:00:00 to
01:00:00 hours. They may decide
tomorrow that that time is no longer
good and change it to 23:0:00 to
05:00:00 or something like that. Am I
missing something??
You have no problem in the time definition part. You may want to play with the code that sends out the report.
// current time
$timeNow = time();
// fetch user time options from database
$timeOn = [from the database];
$timeOff = [from the database];
// convert times to seconds from epoch
$timeOn = strtotime($timeOn);
$timeOff = strtotime($timeOff);
// if database time is in timestamp format,
// only the hour, minutes and second information is needed
$timeOn = mktime(date("H", $timeOn), date("i", $timeOn), date("s", $timeOn));
$timeOff = mktime(date("H", $timeOff), date("i", $timeOff), date("s", $timeOff));
// if time on is higher than time off, time on is of yesterday
if($timeOn > $timeOff){
$timeOn = strtotime("-24 hour", $timeOn);
}
// decide on report sending
if($timeNow >= $timeOn && $timeNow <= $timeOff){
// Send report
} else {
// Do not send report or reschedule the report
}
Any two times in hh:mm:ss format are going to be within a 24 hour time period, as you state. So unless you actually store a date, I am not sure how you can do this.
If I understand correctly, a start time of 23:00:00 and an end time of 04:00:00 should be acceptable to you (this just means 5 hour work shift)? If this is acceptable, then can you give me an example of unacceptable input?
Perhaps you want to check that the end time is within 12 hours of the start time? That should be feasible.

Categories