I have the following function
function status($open, $lunch, $close)
{
if(date('H') < $open || date('H') > $close)
{
$GLOBALS['status'] = "Closed";
$GLOBALS['color'] = "rgba(255,0,0,1)";
}
elseif(date('H') == ($close-1))
{
if(date('i') > 29)
{
$GLOBALS['status'] = "Closing";
$GLOBALS['color'] = "rgba(255,255,0,1)";
}
else
{
$GLOBALS['status'] = "Open";
$GLOBALS['color'] = "rgba(0,255,0,1)";
}
}
else
{
if(date('H') == $lunch)
{
$GLOBALS['status'] = "Lunch";
$GLOBALS['color'] = "rgba(0,0,255,1)";
}
else
{
$GLOBALS['status'] = "Open";
$GLOBALS['color'] = "green";
}
}
}
and after 10:00PM it is supposed to return the status of closed and right now its 10:13 and it's still returning open, I've gone through the code and cannot seem to find the problem.
Could someone take a look and see where my code is failing??
Try using >= on closing.
if(date('H') < $open || date('H') >= $close)
{
$GLOBALS['status'] = "Closed";
$GLOBALS['color'] = "rgba(255,0,0,1)";
}
What happened is you set 10:00 PM which is 22. If the current time is 10:13 which is still not greater than 22. That's why it did fail the condition. Should be greater than or equal, then, it's closed.
try this
function status($open, $lunch, $close)
{
$hour = date('H');
$minute = date('i');
if($hour < $open || $hour >= $close)
{
$GLOBALS['status'] = "Closed";
$GLOBALS['color'] = "rgba(255,0,0,1)";
}
else if($hour==$lunch)
{
$GLOBALS['status'] = "Lunch";
$GLOBALS['color'] = "rgba(0,0,255,1)";
}
else
{
$GLOBALS['status'] = "Open";
$GLOBALS['color'] = "green";
}
if($hour == ($close-1) && $minute>29)
{
$GLOBALS['status'] = "Closing";
$GLOBALS['color'] = "rgba(255,255,0,1)";
}
}
NOTE : also set your default timezone like
date_default_timezone_set('your timezone');
//example
date_default_timezone_set('Asia/Kolkata');
Make sure you have the correct time zone set,
date_default_timezone_set('Asia/Colombo');
List of timezones supported in php can be found here
Related
I want to get birthday dates in form of 'Today' ,'Tomorrow','Yesterday' form.
1.if candidate birthday was on 26-july-1991 it should be print 'yesterday'
2.if candidate birthday is on 27-july-1991 it should be print 'today'.
3.if candidate birthday will on 28-july-1991 it should be print 'tomorrow'.
code
$current = strtotime(date("Y-m-d"));
$date = strtotime("2014-07-24");
$datediff = $date - $current;
$difference = floor($datediff/(60*60*24*365));
if($difference==0)
{
echo 'today';
}
else if($difference > 1)
{
echo 'Future Date';
}
else if($difference > 0)
{
echo 'tomarrow';
}
else if($difference < -1)
{
echo 'Long Back';
}
else
{
echo 'yesterday';
}
Maybe a bit complicated solution, but here I compare month num and date num:
$current_month = date("n");
$current_day = date("j");
// date of birth
$dob = strtotime("1991-07-26");
$dob_month = date("n", $dob);
$dob_day = date("j", $dob);
if ($current_month == $dob_month) {
if ($current_day == $dob_day) {
echo 'TODAY';
} elseif ($current_day == $dob_day + 1) {
echo 'YESTERDAY';
} elseif($current_day == $dob_day - 1) {
echo 'TOMORROW';
} else {
echo 'IN this month';
}
} elseif ($current_month < $dob_month) {
echo 'In future';
} else {
echo 'Long back';
}
Use the php Date and Time class
Something like this:
$today=new DateTime("2017-07-27");
$other_day=new DateTime("2017-07-28");
$check = $today->diff($other_day);
$difference = (integer)$check->format( "%R%a" );
echo $difference;
just delete * 365 and your code should work
I am trying to count number of working days available for particular hours set. here i just need to exclude Sundays by the following php script. if this script find a Sunday this should increase the count. its working but,
This script is capable to exclude first 'Sunday' but not the 'second' and 'third'.
kindly give me a solution to correct this
function testRange() {
$phone_Quantity = 0;
$phone_Quantity = $_POST['phoneQuantity'];
if ($phone_Quantity > 0 && $phone_Quantity <= 300) {
return 300;
} elseif ($phone_Quantity >= 301 && $phone_Quantity <= 600) {
return 600;
} elseif ($phone_Quantity >= 601 && $phone_Quantity <= 900) {
return 900;
} elseif ($phone_Quantity >= 601 && $phone_Quantity <= 1200) {
return 1200;
} elseif ($phone_Quantity >= 1201 && $phone_Quantity <= 1500) {
return 1500;
} elseif ($phone_Quantity >= 1501 && $phone_Quantity <= 1800) {
return 1800;
}
}
echo testRange();
$query_to_get_hours = "SELECT
cdma_filtering_target_hours.Target_hours
FROM
cdma_filtering_target_hours
WHERE
cdma_filtering_target_hours.No_of_units='" . testRange() . "'
";
$query_to_get_hours_query = $system->prepareSelectQuery($query_to_get_hours);
foreach ($query_to_get_hours_query as $THours) {
$targeted_hours = $THours['Target_hours'];
}
$hid = 24; // Hours in a day - could be 24, 48, etc
$days = round($targeted_hours / $hid);
for ($xdays = 0; $xdays < $days; $xdays++) {
if (date('l', strtotime(date('y-m-d', strtotime("+$xdays days")))) == 'Sunday') {
$days++;
break;
}
}
Why do you converting +$xdays string representation twice?
If you comment your if statement and add next line
echo date('l', strtotime("+$xdays days"));
you can clearly see that it works.
I have 4 dates with me, 2 for campaign one of them is campaign start date and the other one is for campaign end date and the same thing goes for filter dates. Now I want to check how many days my campaign runs in between a given filter start date and filter end date . I am using these conditions but
if ($datecs != FALSE && $datece != FALSE && $datefs != FALSE && $datefe != FALSE) {
if( ($datecs >= $datefs) && ($datece <= $datefe)) {
// campaign start and end date between filterdates
$tot_days = $this->get_days($datecs,$datece);
if($tot_days != 'ok'){
return $tot_days;
} else {
return 0;
}
} elseif (($datecs <= $datefs) && ($datece >= $datefe)) {
//filterdates between campiagn start and end
$tot_days = $this->get_days($datefs,$datefe);
if($tot_days != 'ok'){
return $tot_days;
} else {
return 0;
}
} elseif(($datecs <= $datefs ) && ( $datece <= $datefe ) && ($datece >= $datefs)) {
// campaign end date between filterdates
$tot_days = $this->get_days($datefs,$datece);
if ($tot_days != 'ok'){
return $tot_days;
} else {
return 0;
}
} elseif(( $datecs>= $datefs) && ($datece >= $datefe) && ($datecs <= $datefe )) {
// campaign start date between filterdates
$tot_days = $this->get_days($datecs,$datefe);
if (($tot_days != 'ok')) {
return $tot_days;
} else {
return 0;
}
}
}
datecs is the campaign start date and datece is campaign end date .datefs is the filter start date and datefe is filter end date. And get_days function gets me the number of days this get_days function I am using
function get_days($date1,$date2)
{
if($date2 > $date1)
{
$interval = $date1->diff($date2);
return $interval->days+1;
//we add one because it will not calculate the starting date if we substract.
//It will give the number of days to reach last date.
} else if($date2 = $date1) {
return 1;
}
else
{
return "ok";
}
}
Please Let me know if I am going wrong some where.
I rewrite your get_days function :
function get_days($date1,$date2)
{
if($date2 < $date1)
{
$interval = $date1->diff($date2);
return $interval->days;
} else if($date2 == $date1) {
return 1;
}
else
{
return 0;
}
}
I don't know if that what you want but i found 2 mistakes :
1) you are using = and not == to compare
2) you swapped in the comparaison : it's $date2 < $date1
Also it's better to return 0 is it failed> Try to not mix return type in a same function, it will make your code easier to understand.
This worked for me.
EDIT
In your second condition (second line) You are doing :
if ($datecs >= $datefs) {
get_days($datecs,$datece); }
And then if the function get_days : if ($date2 > $date1)
That means you are doing
if ($datecs >= $datefs)
then if ($datecs < $datefs)
//do some stuff
You should look into using php carbon. It's a great library that has a lot of functionality that could make this simpler and easier to read and understand.
http://carbon.nesbot.com/docs/
High Season:
April 28 - September 30,
December 27 - January 3
Low Season:
October 1 - December 26,
January 4 - April 27
I gave a date to check: 2014-02-18 and I want to have TRUE or FALSE in case of which season includes it. How to do it regardless of the current year?
Try with simple date comparing:
function is_high_season($date) {
$md = gmdate('m-d', strtotime($date));
return
('03-28' <= $md && $md <= '09-30') ||
('12-27' <= $md && $md <= '12-31') ||
('01-01' <= $md && $md <= '01-03');
}
demo
$day = $d->format('j');
$month = $d->format('n');
if($month == 1 && $day <= 3) {
return 'high';
} elseif $month < 4 || ($month == 4 && $day < 28)) {
return 'low';
} elseif($month == 4 && $day >= 28) {
return 'high';
} elseif($month < 10) {
return 'high';
} elseif($month < 12 || ($month == 12 && $day < 27)) {
return 'low';
} elseif($month == 12 && $day >= 27) {
return 'high';
}
$Date = date('Y-m-d');
$Date=date('Y-m-d', strtotime($Date));;
//echo $Date; // echos today!
$hiDateBegin = date('Y-m-d', strtotime("28/04/2014"));
$hiDateEnd = date('Y-m-d', strtotime("30/09/2014"));
$hiDateBegin2 = date('Y-m-d', strtotime("27/12/2014"));
$hiDateEnd2 = date('Y-m-d', strtotime("03/01/2015"));
$lowDateBegin = date('Y-m-d', strtotime("01/10/2014"));
$lowDateEnd = date('Y-m-d', strtotime("26/12/2014"));
$lowDateBegin2 = date('Y-m-d', strtotime("04/01/2015"));
$lowDateEnd2 = date('Y-m-d', strtotime("27/04/2015"));
if (($Date > $hiDateBegin) && ($Date < $hiDateEnd))
{
echo "is Hi Season";
}
if (($Date > $hiDateBegin2) && ($Date < $hiDateEnd2))
{
echo "is Hi Season";
}
if (($Date > $lowDateBegin) && ($Date < $lowDateEnd))
{
echo "is Low Season";
}
if (($Date > $lowDateBegin2) && ($Date < $lowDateEnd2))
{
echo "is Low Season";
}
else
{
echo "Date doesn't fall in a season!";
}
Ok sorry about the wording on this one but it's doing my head in, I need to find the earliest delivery date,
$useStartDate = mktime(0, 0, 0, date("m") , date("d")+1, date("Y"));
I have 2 functions
The first checks to see if a date is a weekend or a Monday and increments it accordingly
function checkWeekend ($CheckDateDate){
if (date("w", $CheckDateDate) == 1) { //monday
return strtotime('+1 day', $CheckDateDate);
}
else if (date("w", $CheckDateDate) == 0) { //sunday
return strtotime('+2 day', $CheckDateDate);
}
else if (date("w", $CheckDateDate) == 6) { //saturday
return strtotime('+3 day', $CheckDateDate);
}
else {
return 0;
}
}
The second check to see if the date is in my database of holidays
function checkHoliday ($CheckDateDate) {
$result = mysql_query("SELECT * FROM tblNonDeliveryDates Where NonDeliveryDate = '$CheckDateDate'");
if (mysql_num_rows($result) > 0) {
return strtotime('+1 day', $CheckDateDate);
}
else {
return 0;
}
}
Now what I want to do is check both functions until they both return 0, Where I'm having trouble going back and checking the date is not a weekend after it's been incremented because it's a Holidays. This is what I have, but I know that it's wrong.
$CheckDate = $useStartDate;
while ($Checkdate > 0)
{
$LastChecked = $CheckDate;
$Checkdate = checkWeekend($CheckDate);
$Checkdate = checkHoliday($CheckDate);
}
echo $LastChecked;
Hope that's clear.
You can try like this
$CheckDate = $useStartDate;
while ($Checkdate > 0)
{
$weekend = false;
$holiday = false;
if( checkWeekend($Checkdate) != 0)
$weekend = true;
else if( checkHoliday($Checkdate) != 0)
$holiday = true;
else
$Checkdate = 0;
if( $weekend )
$Checkdate = checkWeekend($Checkdate);
else if( $holiday )
$Checkdate = checkHoliday($Checkdate);
$LastChecked = $Checkdate;
}
echo $LastChecked;