PHP time values when Daylight Saving Time (DST) - php

I am developing a system to sign up, which should add up all the time someone has been online in my server. I want to take into account also those extra hours that have to be added or subtracted from the total when time changes due to DST.
It is easy to subtract an hour in the third Sunday of March, because I only have to check if the signing time was before 2AM and the sign-out time after 3AM; it is also easy when, in October, someone signs in before 2AM and signs out after 3AM, in that case, I only have to add an hour.
But what about signing in between 2 and 3AM (let's say 2:30) on the last Sunday of October? How would I know if it is the first time it is 2:30, or the second time? In the first case I should add half an hour to the total, whereas in the second case I should leave it as it was before.
Does PHP's time() function give different values when it is 2:30 for the first time than when it is for the second time? I assume it should, as it returns the number of seconds since the Unix Epoch, and it shouldn't be the same for both cases, but I would like to confirm it.
Edit 1:
This is the code to calculate the two cases I mentioned where it was easy enough:
(timeChangingDates is a function coded by myself, which given the year, it returns an array of length 2. In the first position there will be the last Sunday of March, in which time changes, and in the second position, the last Sunday of October)
($startTime and $endTime are DateTime objects in which sign-in and sign-out datetimes are stored)
// If sign-in time is before 2AM and sign-out time after 3AM add an hour(October)
if ((strtotime($startTime->format('Y-m-d H:i:s')) < strtotime(new DateTime($startTime->format("Y").'-10-'.timeChangingDates($startTime->format("Y"))[1].' 1:59:00')->format('Y-m-d H:i:s'))) && (strtotime($endTime->format('Y-m-d H:i:s')) > strtotime(new DateTime($startTime->format("Y").'-10-'.cambiosHorarios($startTime->format("Y"))[1].' 3:00:00')->format('Y-m-d H:i:s')))) {
$totalTime = $totalTime + 60;
// If sign-in time is before 2AM and sign-out time after 3AM subtract an hour(March)
} else if ((strtotime($startTime->format('Y-m-d H:i:s')) < strtotime(new DateTime($startTime->format("Y").'-3-'.timeChangingDates($startTime->format("Y"))[0].' 1:59:00')->format('Y-m-d H:i:s'))) && (strtotime($endTime->format('Y-m-d H:i:s')) > strtotime(new DateTime($startTime->format("Y").'-3-'.cambiosHorarios($startTime->format("Y"))[0].' 3:00:00')->format('Y-m-d H:i:s')))) {
$totalTime = $totalTime - 60;
}
I haven't tried any way to do the third case mentioned yet, as I cannot simulate time change on Windows. That is why I'd like to know if just using time() would work.

Related

Calculating consecutive days

when a user logins into his account I want to calculate since how many consecutive days this user has loged in.
For this I store the a timestamp in $loginSince and calculating the consecutive days with this formula:
$consecutiveDays = (int)((time() - $loginSince) / DAY) + 1;
The problem is that this counts by seconds, meaning:
if the user logs in at 17:00 (5pm) and on the next day at 15:00 (3pm) the counter is still 1
if the user logs in at 8:00 (8am) and on the next day at 20:00 (8pm) the counter was resetet (beacause the gap between the two logins is greater than 24h and is again 1
I'd like to get real consectuve days, meaning if a user logs in as the following times:
Day 1: 00:01
Day 2: 23:59
Day 3: 15:00
The counter should say 3, because he loged in at three consectutive days. With my current formular this wont work as the the counter would reset between day 1 and 2.
Can anyone give an advise how to handle this please.
Perhaps use date('z'), as that should just give you the day and not the time?
To post the solution I made. It's based on the idea of Ed Heal to always compare fixed times. I had chosen midnight. After that I build the interval of this to also handle year changes and streaks for more than a year
$loginSince = $this->dailyLoginSince($account);
// Create midnight date of the moment the login-streak started
$loginSinceMidnight = strtotime(date('Y-m-d',$loginSince).' 00:00:00');
// We create the difference between the two dates, both at midnight
$numericDayLoginSince = new DateTime();
$numericDayLoginSince->setTimestamp($loginSinceMidnight);
$numericDayNow = new DateTime();
$timestamp = strtotime('today midnight');
$numericDayNow->setTimestamp($timestamp);
$interval = $numericDayLoginSince->diff($numericDayNow);
$loginSinceDays = $interval->format('%a'); // %a = Number of days
// +1 because it seems to be more thrilling, that we already got one day
// by just logging in for the first time.
return $loginSinceDays + 1;

How to stop reservations before today

I'm trying to create a reservation system for a games library of some sort.
Users should not be allowed to reserve the game for a day before today.
I tried to do this by changing the date chosen by the user for the start of the reservation to a timestamp. Then I would set the date for today, change it to a timestamp and check if the date chosen by the user is less than todays timestamp.
Here is the code:
$timestamp = strtotime($ReservationStart);
$todaystamp = (strtotime('yesterday midnight'));
if ($timestamp < $todaystamp) {
die("The date you've chosen is before today, please choose a valid date");
}
I thought this would work but it this code only stops reservations for 2 days past and behind rather than yesterday and behind.
Any ideas on how to get it to work for yesterday?
What you are looking for is to use midnight rather than yesterday midnight.
$timestamp = strtotime('midnight');
It seems that the strtotime works backwards, so yesterday midnight would be yesterday at 00:00, so it allows all 24 hours of the next day (yesterday) to be allowed to book in.
midnight would go to the current day's midnight (starting at that current date's 00:00.
If you were worried about getting today's midnight, that would be:
$timestamp = strtotime('today midnight');
So it's quite easy to understand once you learn that.

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

How do you rotate text strings contingent on the time of day, in PHP?

I am trying to rotate phone numbers depending on the time of day. If the time is between 8am and 5pm, it should show the 888 number. If it is after 5pm but before 8am it should show the 777 number. This is what I got thus far, but it only shows the 777 number as my output, even though the time is between the hours of 8am and 5pm.
$dayPhoneNumber = "1-888-888-8888";
$nightPhoneNumber = "1-777-777-7777";
$currentPhoneNumber = "";
$nowHour = date("H");
$startHour = 8;
$endHour = 17;
if ($nowHour >= $startHour && $nowHour <= $endHour){
echo $dayPhoneNumber;
} else {
echo $nightPhoneNumber;
}
My guess is your server's time is not the same as your computer's time. If you're using a hosting solution, your server may very well be across the country or on a different continent, meaning the time will be different.
If you just need to worry about one location and not where the end user is, then this should be easy to do. If you're on the West Coast of the US
date_default_timezone_set("America/Los_Angeles");
$hour = date("H", time());
Here is a list of Time Zones: http://www.php.net/manual/en/timezones.php
If you need to know the end user's time zone when determining the hour, here's a discussion on doing that: How to get client's timezone?
If you're dealing with time zone issues, you may also want to retrieve the time using UTC, and then add or subtract whatever number of hours to get to the correct time zone: get UTC time in PHP

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