I am trying to determine if a day and time are between two others, I have the following...
$currentdate = date("N h:i:s A");
This returns the day of the week as a number and then the current time in 24 hour format.
I want to check if the $currentdate is between 9am on a Friday and 9am on a Monday.
What is the best way to tackle this?
I believe this should give you what your asking for but I'm sure there are better ways to implement. So basically the time has been converted into an INT for comparing and the hours are configured not to have a leading zero hence why $timeOne and $timeTwo is shorter. I've then used an if statement to test days and time on that specific day leaving you a slot to add your code if those conditions are met.
function checkDayTime() {
$day = date(w); //0 (for Sunday) through to 6 (for Saturday)
$timeOne = 90000;
$timeTwo = 90000;//Added for easier reading
$currentTime = (int) date('Gis'); //Time as INT 00000 > 240000
if (($day == 5 && $currentTime > $timeOne) || ($day == 6 || $day == 0) || ($day == 1 && $currentTime < $timeTwo)) {
//Between those hours
return TRUE;
} else {
//Not between those hours
return FALSE;
}
}
Just removed the extra if statement as it was not needed
Related
The idea is to play an alarm from html/php web-page via a TV hanging near the school canteen reminding students to wash their hands. The TV is used as a tabloid screen for school events and news.
The piece of code I'm using on this page is as follows:
<?php
$hour = date('G');
$day = date('1..5'); // 1..7 for Monday to Friday
if (($hour >= 11.05 && $hour <= 11.35) // 5am - 7am
|| ($hour >= 12.15 && $hour <= 12.30) // 10am - 12 noon
|| ($hour >= 21.10 && $hour <= 21.30) // 10am - 12 noon
|| ($day == 1-5) // Monday - Friday
) { ?>
<audio src="Audio/sirena.mp3" autoplay="true" loop="loop">
<?php } ?>
This only plays it once the page is uploaded onto the server, and if it falls within the times above. Otherwise, it stays silent. And, strangely it plays only on my home PC on Chrome/IE/Mozilla and it doesn't play completely on school PCs.
Bear in mind the page auto refreshes itself every 5 min.
Would appreciate if someone gave a hint on event listeners or anything else.
There are a couple of problems with your code.
In $day = date('1..5');, '1..5' isn't a valid format for date(). That will just return the string 1..5. You want date('w').
date('G') returns the hour in 24-hour format, so $date will be a string representation of an integer. In your if condition you are comparing it to floats. Since all of the different time conditions are various decimals above the same integers, $date will never match any of them. For example, if the time is 11:30 AM, $date will be '11', which is not between 11.05 and 11.35. (Obviously, neither is '10' or '12'.)
In the last part of the if condition, you have || ($day == 1-5). There are two problems with that. First, 1-5 does not mean "between one and five"; it means "one minus five". And second, the fact that this is an additional non-nested or condition means that it negates all of the various time comparisons, because whenever any part of an or condition is true, the entire condition evaluates to true. If it was || ($day >= 1 && $day <= 5), then the if would be true all day every Monday through Friday. As it is, it's true all day only on Thursday.
I added an updated version that I think will do what you want it to (based on the comments in your code). I used some kind of strange indentation in the if condition to hopefully show the grouping a little better. Basically it needs to check the day and check a group of time conditions. Note the different format for time ('Gi'), which gets the hour and minute in a format that can be compared to integer values in the if condition.
$day = date('w');
$time = date('Gi');
if ( ($day > 0 && $day < 6) // Monday - Friday
&& ( // AND
($time >= 1105 && $time < 1135) // time range 1
|| // OR
($time >= 1215 && $time < 1230) // time range 2
|| // OR
($time >= 2110 && $time < 2130) // time range 3
)
)
echo '<audio src="Audio/sirena.mp3" autoplay="true" loop="loop">';
I am practicing with dates in php. I a bit of a newbie so bear my ignorance
I am trying to see when a time is before noon.
So I have a variable coming in with this format 2014-03-07 13:28:00.000
I get the time like this
$submissonTime = date('H:i:s', strtotime($value['job_submission_date']));
then I want to set another variable as $noon and i am doing this:
$noon = date('H:i:s', '12:00:00.000');
However the value of noon is 12:00:12
what i want to do is basically:
if($submissionTime <= $noon){
//do my stuff
}
NB I want to enter the if statement when even when it is 12:00:00 and stop entering when it is 12:00:01
Any help please?
Try
$noon = date('Y-m-d 12:00:00'); // today noon with date
$submissonTime = date('Y-m-d H:i:s', strtotime($value['job_submission_date']));
if(strtotime($submissonTime) <= strtotime($noon)){
//do my stuff
}
if you want to compare only time use both format
$noon = date('12:00:00');
$submissonTime = date('H:i:s', strtotime($value['job_submission_date']));
if (date("A") == "AM")
{
// AM-Code
} else {
// PM-Code
}
Why don't you go with only one string of code getting the hour?
$Hour = date("G"); //24-hour format of an hour without leading zeros
if($Hour < 12) {
// do the code
}
Or in your case
$Hour = date("G", strtotime($value['job_submission_date']));
update
If you need 12:00:00 and not 12:00:01 and later on, you will need to define minutes and seconds:
$Hour = date("G"); //24-hour format of an hour without leading zeros
$Minute = intval(date("i")); // will give minutes without leading zeroes
$Second = intval(date("s"));
if(($Hour < 12) || ($Hour == 12 && $Minute == 0 && Second == 0)) {
// do the code
}
This question already has answers here:
How to get weeks starting on sunday?
(10 answers)
Closed 9 years ago.
$date = 'some date';
$todays_week = date('W',strtotime($date));
This code gives me the week number considering Monday as the first day of the week. I want to get the week number considering Sunday as the starting day of week. How do I do that?
The most elegant solution is to simply add 1 whole day to the input:
$todays_week = date('W', strtotime($date) + 60 * 60 * 24 );
The only case that this doesn't work is where it is the 31st of December an it is also a Sunday. In this case, the answer will incorrectly say 1. You'll have to check for this.
As I know there is not any option to change the starting day. So, you can check if the current day is Sunday and if so, increment the week number with 1:
function getIsoWeeksInYear($year) {
$date = new DateTime;
$date->setISODate($year, 53);
return ($date->format("W") === "53" ? 53 : 52);
}
function getWeek($date) {
$week = date('W',strtotime($date));
$day = date('N',strtotime($date));
$max_weeks = getIsoWeeksInYear(date('Y',strtotime($date)));
if($day == 7 && $week != $max_weeks) {
return ++$week;
} elseif($day == 7) {
return 1;
} else {
return $week;
}
}
echo getWeek('2012-12-30');
How do I write a function in PHP that returns the date of delivery without Saturdays, Sundays and holidays?
For example: for same product delivery is in 3 days for others in 5 days.
If I do a command today I will receive my command in 3 days without Saturdays, Sundays and holidays.
If I do my command now "29/12/2011" for a product "3j" I will receive your command at "4/1/2011"
In the database I have just how much the day for every type product is.
For example:
prod1: 3day
prod2: 5day
prod3: 7day
I tried to do a function but failed.
This code (slightly altered) is used in production for one of my sites:
// $dt = date of shipping, $numdays = expected number of days in transit
function realDeliveryDate($dt, $numdays)
{
$holidays = array("05/30/2011","07/04/2011","09/05/2011","11/24/2011","11/25/2011","12/25/2011","12/31/2011","01/01/2012","05/28/2012","07/04/2012","09/03/2012","11/22/2012","11/23/2012","12/25/2012");
$checkday = strtotime($dt." +".$numdays." days");
// check if it's a holiday
while(in_array(date("m/d/Y",$checkday), $holidays)) {
$checkday = strtotime(date("m/d/Y",$checkday)." +1 day");
}
// make sure it's not Saturday
if (date("w",$checkday) == 6) {
$checkday = strtotime(date("m/d/Y",$checkday)." +2 days");
}
// make sure it's not Sunday
if (date("w",$checkday) == 0) {
$checkday = strtotime(date("m/d/Y",$checkday)." +1 day");
}
// make sure it's not another holiday
while(in_array(date("m/d/Y",$checkday), $holidays)) {
$checkday = strtotime(date("m/d/Y",$checkday)." +1 day");
}
return $checkday;
}
This is a really complex subject that you can't really fix with a simple function. You are talking about business days calculation strategies and they involve a lot of thinking.
If you want to process only business days and not business hours, then it get slightly easier.
The first step is to create an array of working days or an array of non working days. For example:
//Build the days based of weekends
$nonWorkingDays = array();
foreach($iDate = 0; $iDate < 365; $iDate++){
$date = strtotime('today +'.$iDate.' day');
if(date('w', $date) == 0 || date('w', $date) == 6){
$nonWorkingDays = date('Y-m-d', $date);
}
}
//Add the holidays
$nonWorkingDays[] = '2011-12-25';
$nonWorkingDays[] = '2012-01-01';
//Determine the date of delivery
$daysToDelivery = 6;
$deliveryDate = time();
while($daysToDelivery > 0){
$deliveryDate = time() + (24*60*60);
if(!in_array(date('Y-m-d', $deliveryDate), $nonWorkingDays)){
$daysToDelivery--;
}
}
Take a look at:
http://php.net/manual/en/function.localtime.php
This will return you a weekday based on the time you provide. So if you current time is X and you need to deliver in 2 days you would do localtime(X + 2*24*3600). The resulting struct will contain the weekday which you can use to move the time forward or backward as you need.
I have debugged this legacy code, and would like a sanity check on it.
The purpose of it is to allow someone to choose a delivery frequency for shipping a product. If someone wants their product Every Other Week, the system needs to determine if they should get an order next week, or two weeks from now. We call it A week, or B Week.
Keep in mind I did not write this, I am just trying to make sense of it and would like some help evaluating its accuracy:
if (date("l") == "Monday" ) {
$start = 0;
} else if (date("l") == "Tuesday" || date("l") == "Wednesday" || date("l") == "Thursday" || date("l") == "Friday" || date("l") == "Saturday"|| date("l") == "Sunday") {
$start = -1;
}
// if changing to every other week set to next week's a/b-ness
$a_week_tid = 34;
$b_week_tid = 35;
$every_other_week_frequency_id = 32;
if ($delivery_frequency == $every_other_week_frequency_id) {
$julian = (int) (strtotime('Monday +' . $start . ' week') / 86400);
$julian_week = ($julian-4) / 7;
if ($julian_week % 2) {
$today_a_or_b = $b_week_tid;
$next_week_a_or_b = $a_week_tid;
$a_or_b_week_string = '(A Week)';
} else {
$today_a_or_b = $a_week_tid;
$next_week_a_or_b = $b_week_tid;
$a_or_b_week_string = '(B Week)';
}
} else {
$next_week_a_or_b = NULL;
$a_or_b_week_string = NULL;
}
This code is not commented or documented. The part that confuses me is:
Why is 4 subtracted from Julian, then divided by 7?
If today is Monday, $julian_week is 2129, and 2129 % 2 evaluates TRUE. Is that correct?
Is this even how it should be done? Can't I rewrite this using date('w') a lot easier?
Yeah using date would totally be easier, plus it takes into account leap years, daylight saving time, all that extra stuff you don't want to have to deal with.
if (date('W')%2==1)
That's SOOOO much easier to maintain than the above.
I don't believe you can use date("W") in this case. According to the ISO calculation, on occasion, there will be years with 53 weeks. In those years, Week 53 is followed by Week 01, both odd numbers, and an A/B calculation based on Even/Odd ISO week number would result in two successive A or B weeks.
The original calculation determines the number of days from the UNIX epoch of the present Monday, or of the most recent Monday if today is not a Monday. The -4 causes the A/B week labels to change on Thursdays. Even/oddness of a week is determined from a fixed date (the Unix Epoch), so there will be no discontinuity in the oscillation of A/B-ness using the original code.
The ISO standard for week one in a year is that it is the week that the first Thursday of the year falls. This is the reason for the 4 subtracted from the Julian date. The week number is then found by dividing by 7.
Again the ISO standard implies that week number cannot be greater than 53. I don't understand how your figure of 2129 can arise. However the div operator will not evaluate TRUE for this figure. Checking the div operator on the week number is the way of determining whether you are in week a or b. If it is before Thursday, it is quite likely that the number will be 1 less than you anticipate.
The coding looks fairly good to me, though I have not stepped through all of it. It does look correct.
Using W on consecutive Fridays, mod by 2. Both lines output 1. So doing it this way will occasionally fail.
echo date('W',strtotime('2016-01-01'))%2;
echo date('W',strtotime('2016-01-08'))%2;
Just a simple way.
<?php
$weekNumber = date("W");
echo 'Week number:',$weekNumber;
if($weekNumber&1) {
echo '<strong>Week A.</strong>';
} else {
echo '<strong>Week B.</strong>';
}
?>
$day = '2019-11-10';
$date = new DateTime($day);
$dayOfMonth = $date->format("j"); // month days 1 - 30
$weekNumber = ceil($dayOfMonth / 7); // get the week number
if ($weekNumber % 2 == 0) { //if week number is even
echo "Even Week";
} else {
echo "Odd Week";
}
**// output Even Week**