I have a piece of PHP program that would supposedly identify the Shift time of a user when they login.. see below actual script condition.
$dt = new DateTime(date('H:i:s'));
$dt->setTimezone( new DateTimeZone("Asia/Manila") );
$timeNow = $dt->format('H:i:s');
if (strtotime($timeNow) >= mktime(07,0,0) && strtotime($timeNow) < mktime(15,0,0)){
$shift = "2nd [0700H - 1500H]";
}elseif(strtotime($timeNow) >= mktime(15,0,0) && strtotime($timeNow) < mktime(23,0,0)){
$shift = "3rd [1500H - 2300H]";
}else{
$shift = "1st [2300H - 0700H]";
}
The script above is working but there are records that falls into wrong shift.. for example a user access the page in 7:10AM which means should be in 2nd Shift but instead.. it falls into 1st Shift
I don't know what have I missed in this control flow.. so if anyone from here can help and share ideas that would be very much appreciated. Thank you!
There's a difference between using DateTime::setTimezone() and applying a timezone in the DateTime constructor. The former, converts the existing time to the DateTimezone specified as the argument to DateTime::setTimezone(), and the latter, assumes that the supplied formatted date is already in that timezone.
You're saying you want to know whether right now it's between 7 am and 3 pm in Manila, or between 3 pm and 11 pm, etc... So the idea is to compare only within that timezone. There is no need to do any timezone conversion here whatsoever.
$now = new DateTime("now", new DateTimeZone("Asia/Manila"));
$shift2 = new DateTime("7:00 AM", new DateTimeZone("Asia/Manila"));
$shift3 = new DateTime("3:00 PM", new DateTimeZone("Asia/Manila"));
$shift1 = new DateTime("11:00 PM", new DateTimeZone("Asia/Manila"));
if ($now >= $shift2 && $now < $shift3) {
// It's shift 2
} elseif ($now >= $shift3 && $now < $shift1) {
// It's shift 3
} else {
// It's shift 1
}
Related
I am trying to hide a message between 2 particular timings and rest of the timings it should display.
the timings are
07:30am to 10:30am
and
18:30pm to 00:00am midnight
currently i did the following code which works ok for 07:30am to 10:30am it does the job but now how do i combine the 18:30pm to 00:00am midnight??? please someone help me out with some good logic.
<?php
date_default_timezone_set('America/New_York');
$now = new DateTime();
$fromTime = new DateTime();
$fromTime->setTime(07,30);
$toTime = new DateTime();
$toTime->setTime(10,30);
$fromTime1 = new DateTime();
$fromTime1->setTime(18,30);
$toTime1 = new DateTime();
$toTime1->setTime(00,00);
if ( $now < $fromTime || $now > $toTime) {
?>
This is a test message
<?php
}
?>
I tried doing like this
$now < $fromTime || $now > $toTime && $now < $fromTime1 || $now > $toTime1
but it messes up and displays the message even during 07:30am.
Thanks for your help
It's prety simple, to check few dates interval, but, don't make mistakes with PHP logical operators (https://www.php.net/manual/en/language.operators.logical.php) :
if ( ($now >= $fromTime && $now <= $toTime) // If it's on the first interval
|| // Or
($now >= $fromTime1 && $now <= $toTime1) // If it's on the second one
) {
echo 'Appear on the interval'; // I display the message.
}
Also, you've a mistake with your last variable $toTime1, you must to check from 18::00 to 00:00 ... Not from the current day ! (but 00:00 to the next day).
You can edit your code like this, by addind +1 day to your last variable :
$toTime1 = new DateTime();
$toTime1->modify('+1 day');
$toTime1->setTime(00,00);
Like this, your timings are good ;)
Don't forget this code (php) is managed by the server side. So, if your user don't reload the page, your message will be visible during your intervals. Maybe you can use JavaScript for hide the message during your intervals on the client side, in the case that the user remains on the page ;)
Hope I help you.
I tried to substract 15 min. from another time (start-time).
I want to check if the current time is 15 min. befor a meeting starts.
foreach($result->value as & $value) {
$start = $value->Start->DateTime;
$startmeeting = substr($start, 11, -11); //cut time to hour:minute
$now= date('H:i', time());
$min= strtotime('-15 minutes');
$timebefor = date($startmeeting, $min); //Here I want to substract starttime with 15 min
if( $now >= $timebefor && $now <= $startmeeting )
{
//Show yellow warning box
}
}
Is it even possible on this way?
You basically have your solution, but it is untidy, and contains bugs. I think you want to do something like this:
foreach ($result->value as $value) {
$meetingStart = strtotime($value->Start->DateTime);
if (($meetingStart > time()) &&
($meetingStart < strtotime('15 minutes')))
{
//Show yellow warning box
}
}
Simply put: If the meeting is in the future, but less than 15 minutes into the future, you will have to show the yellow warning box.
When programming always pay attention to the names you choose. Notice how I use $nowPlus15Minutes which does clearly indicate what that variable contains. You used $min which isn't very self explanatory. The same problem exists with names like $value and $start. Perhaps $timebefor is a misspelling?
I recommend that you use PHP:DateTime with this. If your system works with external API (such as Google Calendar), I will usually specify the timezone too.
$currentTime = new DateTime("now", new DateTimeZone("Asia/Singapore"));
$reminderTime = new DateTime("2019-08-08T12:00:00.0000000", new DateTimeZone("Asia/Singapore"));
$reminderTime->sub(new DateInterval("PT15M")); // PT means period time, 15 minutes.
// Comparison of DateTime is allowed from PHP 5.2.2 onwards
if($currentTime > $reminderTime) {
// Do something
}
// For DEBUGGING
echo $currentTime->format('Y-m-d H:i:s') . "\n" . $reminderTime->format('Y-m-d
H:i:s');
Refer to DateTime documentation for more information.
I am using PHP, jQuery AJAX and HTML to create a timesheet system, for this the user needs to select 2 dates within 1 month of each other. The system as yet is working and shows (very limited) data.
BUT! When I actually select a date over the month limit (i.e. 2 months further than the start or another year after the start), it still shows the table with the data.
For this I have this check:
$dt1 = new DateTime($_REQUEST['startdate']);
$dt2 = new DateTime($_REQUEST['enddate']);
$diff = date_diff($dt1, $dt2);
// I have tried this the other way around and get the same result...
if($diff->m > 1 || $diff->y > 1)
{
print("<center><strong>Time between dates it too great<br />Please choose another date or time within a month of each other</strong></center>");
die();
}
The dates are passed by a jQuery datepicker object via AJAX, and the dates I use, for example, are passed as such:
11/14/2015 (start date) && 12/14/2015 (end date) - should show data
09/14/2015 (start date) && 12/14/2015 (end date) - should not show data but does
11/14/2015 (start date) && 12/14/2016 (end date) - should not show data but does
There is a check in place that sees if the dates given start before the other and this works, I have tried the same kind of thing for this check, but without success, this check is as such:
function CountDaysBetween($startDate, $endDate)
{
$begin = strtotime($startDate);
$end = strtotime($endDate);
if ($begin > $end) {
echo "start date is in the future! <br />";
return;
} else {
$no_days = 0;
$weekends = 0;
while ($begin <= $end) {
$no_days++; // no of days in the given interval
$what_day = date("N", $begin);
if ($what_day > 5) { // 6 and 7 are weekend days
$weekends++;
};
$begin += 86400; // +1 day
};
$working_days = $no_days - $weekends;
return $working_days + 1;
}
}
Edit
Dates 2 or more months apart within the same year work, tested again and this is the case, but dates into the next year do not
In your first part of the php code, you have put this operator>, but the problem is it means, everything Smaller than 1, not everything that is smaller than one or equal to 1. The easy solution is to change the operators to >=; which means everything that is equal to 1 or smaller than 1.
The date_diff constructs in PHP suck monkeyballs. Far more practical is to use straight comparisons instead:
$dt1 = new \DateTime($_REQUEST['startdate']);
$dt2 = new \DateTime($_REQUEST['enddate']);
$dt1->add(new \DateInterval('P1M'));
echo ($dt1 < $dt2 ? 'Less' : 'More') . ' than a month';
Also please do not use $_REQUEST, it has potentially terrible security issues. You should use $_GET, $_POST or $_COOKIE according to what you explicitly expect.
I am trying to check if the user has been on the site longer then 6 months, if they have to prompt a message.
I cannot get this to work, it keeps thinking 6 weeks has passed even when the date is moved to today.
Currently my code.
$created_at = "2014-12-01 16:58:23";
$sixweek = 604800 * 6;
if(strtotime($created_at) < time() + ($sixweek))
{
$data['needsnewimage'] = 1;
die('6 Weeks has passed ');
}else{
$data['needsnewimage'] = 0;
die('6 Weeks has not passed');
}
Any ideas?
That's not going to work. Consider this: someone signs up for your site TODAY:
$created_at = '2014-12-01 08:00:00'; // "today"
if ($created_at < $today + $sixweek) ...
becomes
if ($today < $today + $sixweek)
if (0 < $sixweek)
and will always be true.
You want
if ($created_at > (time() - $sixweek))
^---------^
Note the changed math
DateTime will make this simple, something like:-
$created_at = new \DateTime("2014-12-01 16:58:23");
$sixWeeks = new \DateInterval('P6W');
if($created_at->add($sixWeeks) < new \DateTime()){
echo "Six weeks has passed!\n";
} else echo "Not yet!\n";
See it working
Reference
I'd like to write a function that returns a boolean true if the request is being made between 8am and 5pm central timw monday-saturday, and false any other time. I know that I will probably be using date() and strtotime()but outside of that, I'm lost. Any pointers?
desired result
if (DuringBusinessHours()) {
// execute this
} else {
// execute this
}
I would suggest something like this:
// create start and end date time
$start = new DateTime('8:00am');
$end = new DateTime('5:00pm');
// get the current time
$now = new DateTime();
// note that you can use the < > operators
// to compare date time objects
if($now >= $start && $now < $end) {
echo 'during business hours';
}