I want to check when the store will be open:
if today is Wednesday and the store is close then it will say will
open on Thursday or Tomorrow.
if not Thursday will go to check Friday and so on but the issue is
it will first check Monday as the number of day is 1.
Then if available the message is the store will open on Monday which is not correct.
So I need to check first the days numbers: 4,5,6,7 then if not available check 1,2,3.
I tried with this but doesn't work.
foreach($storedays as $storeday)
{
if($storeday->storeinfo_id == $item->id)
{
$store_open = \Carbon\Carbon::createFromFormat('H:i', $storeday->open_time)->format('H:i');
$store_close = \Carbon\Carbon::createFromFormat('H:i', $storeday->close_time)->format('H:i');
if ($todayTime >= $store_open && $todayTime <= $store_close && ($storeday->available == 1) && ($storeday->day_id == $today))
{
$is_open = 1;
$when_open = 'now';
break;
}
else
{
if(($storeday->available == 1) AND ($storeday->day_id == $today))
{
$when_open_day = $storeday->day_id;
$open_day = date('l', strtotime("Sunday +$when_open_day days"));
$when_open_time = date('h:i a', strtotime($store_open));
if($storeday->day_id==$today)
{
$open_at = 'now';
}
else
{
$open_at=date('D', strtotime("Sunday +$when_open_day days"));
}
break;
}
else if(($storeday->available == 1) AND ($storeday->day_id < $today))
{
$when_open_day = $storeday->day_id;
$open_day = date('l', strtotime("Sunday +$when_open_day days"));
$when_open_time = date('h:i a', strtotime($store_open));
$open_at=date('D', strtotime("Sunday +$when_open_day days"));
break;
}
}
}
}
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 have 2 dates like this YYYY-mm-dd and I would like to check if these 2 dates are a weekend.
I have this code but it only tests 1 date and I don't know how to adapt it; I need to add a $date_end.
$date = '2011-01-01';
$timestamp = strtotime($date);
$weekday= date("l", $timestamp );
$normalized_weekday = strtolower($weekday);
echo $normalized_weekday ;
if (($normalized_weekday == "saturday") || ($normalized_weekday == "sunday")) {
echo "true";
} else {
echo "false";
}
A couple of hints:
date('N') gives you normalised week day you can test against (no need to use localised strings)
Wrap it all in a custom function and you're done
You can use shorter code to check for weekend => date('N', strtotime($date)) >= 6.
So, to check for 2 dates — and not just 1 — use a function to keep your code simple and clean:
$date1 = '2011-01-01' ;
$date2 = '2017-05-26';
if ( check_if_weekend($date1) && check_if_weekend($date2) ) {
echo 'yes. both are weekends' ;
} else if ( check_if_weekend($date1) || check_if_weekend($date2) ) {
echo 'no. only one date is a weekend.' ;
} else {
echo 'no. neither are weekends.' ;
}
function check_if_weekend($date) {
return (date('N', strtotime($date)) >= 6);
}
Using your existing code, which is slightly longer, following is how you would check for 2 dates:
$date1 = '2011-01-01' ;
$date2 = '2017-05-27';
if ( check_if_weekend_long($date1) && check_if_weekend_long($date2) ) {
echo 'yes. both are weekends' ;
} else if ( check_if_weekend_long($date1) || check_if_weekend_long($date2) ) {
echo 'no. only one date is a weekend.' ;
} else {
echo 'no. neither are weekends.' ;
}
function check_if_weekend_long($date_str) {
$timestamp = strtotime($date_str);
$weekday= date("l", $timestamp );
$normalized_weekday = strtolower($weekday);
//echo $normalized_weekday ;
if (($normalized_weekday == "saturday") || ($normalized_weekday == "sunday")) {
return true;
} else {
return false;
}
}
Merging multiple answers into one and giving a bit extra, you'd come to the following:
function isWeekend($date) {
return (new DateTime($date))->format("N") > 5 ? true : false;
}
Or the long way:
function isWeekend($date) {
if ((new DateTime($date))->format("N") > 5) {
return true;
}
return false;
}
You can use the strtotime(); and date() functions to get the day of the date, and then check if is sat o sun
function check_if_weekend($date){
$timestamp = strtotime($date);
$day = date('D', $timestamp);
if(in_array($day, array('sun', 'sat'))
return true;
else return false;
}
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
I want to get tomorrow date. But if tomorrow is Saturday or Sunday. It will get date in Monday. It will skip Saturday and Sunday. Also if tomorrow is holiday, it will skip the date and get the next date. I have list of holiday date in my database.
I've trying this code. Suppose today is Friday, May 24.
$today = "2013-05-24";
$tommorow = date('Y-m-d', strtotime($today . ' + 1 day'));
$valid = check_valid($tommorow);
while (!$valid){
$tommorow = date('Y-m-d', strtotime($today . ' + 1 day'));
$valid = check_valid($tommorow);
if($valid){
break;
}
}
function check_valid($date){
return true;
$timestamp = strtotime($date);
$day = date('D', $timestamp);
if ($day == "Sat" || $day == "Sun"){
return false;
}
$mysql= mysql_query("SELECT * FROM holiday_data WHERE date = '$date'");
if (mysql_num_rows($mysql) >= 1){
return false;
}
}
I'm using while clause because i assume that there is holiday date that appears in two or three days in a row. For example, holiday is on May 27 and 28, then tomorrow date should be in Wednesday.
Any ideas?
Could you help me please?
If you have another approach to achieve this, I also want to know it.
Thank you,
Here is the code, doing, what you need:
It uses DateTime class and do-while loop:
<?php
function not_valid($date){
$weekday = $date->format('l');
if ($weekday == "Saturday" || $weekday == "Sunday"){
return true;
}
$str_date = $date->format('Y-m-d');
$mysql= mysql_query("SELECT * FROM holiday_data WHERE date = '$str_date'");
if (mysql_num_rows($mysql) >= 1){
return true;
}
return false;
}
$today = new DateTime("2013-05-24");
do{
//add 1 day
$today->add(new DateInterval('P1D'));
}while(not_valid($today));
print $today->format('Y-m-d');
?>
At the beginning it is 24.05.2013 - Friday. Then it adds 1 to it. 25 and 26 are Saturday and Sunday, so it skips them and prints 2013-05-27.
As Syjin have already said, your check_valid() will always return true because you've got an return true; at the beginning of your function.
Move it to the end of your function to return true if the function hasn't returned false before.
This function checks (with your logic) if a timestamp is valid and if not searches for the next valid timestamp. it will return a timestamp of the first valid timestamp possible.
function getNextValidDay($timestamp) {
$day = date('D', $timestamp);
if ($day == "Sat" || $day == "Sun"){
$timestamp += 60*60*24;
return getNextValidDay($timestamp);
}
$mysql= mysql_query("SELECT * FROM holiday_data WHERE date = '$timestamp'");
if (mysql_num_rows($mysql) >= 1){
$timestamp += 60*60*24;
return getNextValidDay($timestamp);
}
return $timestamp;
}
The problem is in the function check_valid(). You have returned true in the very beginning of the function so it will not allow to process remaining code so it should be something like this,
function check_valid($date){
$timestamp = strtotime($date);
$day = date('D', $timestamp);
if ($day == "Sat" || $day == "Sun"){
return false;
}
$mysql= mysql_query("SELECT * FROM holiday_data WHERE date = '$date'");
if (mysql_num_rows($mysql) >= 1){
return false;
}
return true;
}
you should update/increment '+ 1 day' in each loop. change your code to be something like this
$today = "2013-05-24";
$ii_day = 1;
$tommorow = date('Y-m-d', strtotime($today . ' + '.($ii_day).' day'));
$valid = check_valid($tommorow);
while (!$valid){
$ii_day++;
$tommorow = date('Y-m-d', strtotime($today . ' + '.($ii_day).' day'));
$valid = check_valid($tommorow);
if($valid){
break;
}
}
and then on your check_valid function, change your code a bit.
function check_valid($date){
//return true;
$timestamp = strtotime($date);
$day = date('D', $timestamp);
if ($day == "Sat" || $day == "Sun"){
return false;
}
else {
$mysql= mysql_query("SELECT * FROM holiday_data WHERE date = '$date'");
if (mysql_num_rows($mysql) >= 1){
return false;
}
else
return true;
}
}
hopefully it works..:)
You can try this solution, it's based on my working days code that I use in a couple of my projects. It uses Swedish holidays but you can adjust these to the holidays that are observed in your country. Remember that many holidays depend on the current year.
function next_valid_day() {
$year = date('Y');
$easter_date = easter_date($year);
$holidays = array(
mktime(0, 0, 0, 1, 1, $year), // Nyårsdagen
mktime(0, 0, 0, 1, 6, $year), // Trettondedag jul
//strtotime('-3 days', $easter_date), // Skärtorsdagen
strtotime('-2 days', $easter_date), // Långfredagen
strtotime('-1 day', $easter_date), // Påskafton
$easter_date, // Påskdagen
strtotime('+1 day', $easter_date), // Annandag påsk
mktime(0, 0, 0, 5, 1, $year), // 1:a maj
strtotime('+39 days', $easter_date), // Kristi himmelsfärdsdag
mktime(0, 0, 0, 6, 6, $year), // Sveriges nationaldag
strtotime('-1 day', strtotime('next Saturday', mktime(0, 0, 0, 6, 19, $year))), // Midsommarafton
mktime(0, 0, 0, 12, 24, $year), // Julafton
mktime(0, 0, 0, 12, 25, $year), // Juldagen
mktime(0, 0, 0, 12, 26, $year), // Annandag jul
mktime(0, 0, 0, 12, 31, $year) // Nyårsafton
);
$valid_date = false;
$d = 0;
while ($valid_date === false) {
$date = strtotime('+' . ++$d . ' days');
if (!in_array($date, $holidays) && !(date('N', $date) > 5)) {
$valid_date = true;
}
}
return date('Y-m-d', $date);
}
var_dump(next_valid_day());