Show date of next thursday [duplicate] - php

This question already has answers here:
Get the date of next monday, tuesday, etc
(10 answers)
Closed 11 months ago.
I use the following code to display the date of every next thursday.
Now I got the following problem: today is thursday, but I don't want to show the date of the next week until the current day is over. How is that possible?
Every thursday we have duty from 7pm to 10pm. Therefore I need to display todays date for example. After 10pm I can display the date of the next thursday (for example 30.11.)
Current code:
$timestmp = strtotime('next thursday');
setlocale(LC_TIME, "de_DE");
$next = strftime('%A, %d.%m.%Y', $timestmp);

You can have a simple if condition to check if today is Thursday, print today's date. Else, print next Thursday
setlocale(LC_TIME, "de_DE");
if (date('l') == 'Thursday') {
$thu = strftime('%A, %d.%m.%Y');
} else {
$timestmp = strtotime('next thursday');
$thu = strftime('%A, %d.%m.%Y', $timestmp);
}
echo $thu;

if(date('l') == 'Thursday') {
echo 'Today is Thursday! Whola!'
}
else {
$timestmp = strtotime('next thursday');
setlocale(LC_TIME, "de_DE");
$next = strftime('%A, %d.%m.%Y', $timestmp);
}

So you want show today this thursday?
Then pass to strtotime, yesterday's timestamp. Like that
$timestmp = strtotime('next thursday', strtotime('-1 day'));
Then it will seek for next thursday from yesterday, which is today.

The following code grabs the next thursday if its sunday, monday, tuesday or wednesday. It grabs the last thursday if it's firday or saturday. And it will grab the current day if it's thursday today.
$now = new DateTime();
if ($now->format('w') < 4) {
$now->modify('next thursday')
} elseif ($now->format('w') > 4) {
$now->modify('last thursday')
}
echo $now->format('Y-m-d');

Related

Get a date and show it even after midnight for about 2 hours

I should be able to get the first Wednesday of each month, I can do it easily using:
$firstWedNextMonth = date ('d-m-Y', strtotime('first wednesday of next month'));
echo "The first wed of next month is: ".$firstWedNextMonth;
However, considering that the first Wednesday of next month is March 6th, the system must show this date until 3:00:00 on Thursday March 7th, at 3:00:01 instead it must indicate Wednesday, April 3rd. I do not know how to do it. Thank you
I think, this should work. +27 hours is 24 hours + your margin (3 hours). You can test it setting $now to whatever you want, instead of the current time.
$now = time();
$firstWedThisMonth = date ('d-m-Y', strtotime('first wednesday of this month', $now));
$firstWedNextMonth = date ('d-m-Y', strtotime('first wednesday of next month', $now));
$boundaryDateTime = strtotime('+27 hours', strtotime($firstWedThisMonth));
if ($now <= $boundaryDateTime) {
echo "The first wed of next month is: ".$firstWedThisMonth;
} else {
echo "The first wed of next month is: ".$firstWedNextMonth;
}

PHP get yesterdays date unless weekend

I need to get yesterday's date to show the market close date. This only applies to Monday through Friday as the markets are not open on the weekend. Using this format how would I do that?
<?php
$yesterday = date("Y-m-d", mktime(0, 0, 0, date("m") , date("d")-1,date("Y")));
echo $yesterday;
?>
Here's one way to do it:
$date= new \DateTime();
$oneDay = new \DateInterval('P1D');
$date->sub($oneDay);
if ('Sunday' === $date->format('l')) {
$date->sub($oneDay);
}
if ('Saturday' === $date->format('l')) {
$date->sub($oneDay);
}
echo $date->format('Y-m-d');
I just get today's date, subtract one day, and check to see if it is a Sunday. If so, subtract a day. Then I do the same thing for a Saturday. Ultimately you end up with the last weekday.
Keep in mind this is different then business day's as that Friday may be a holiday and the markets may be closed.
You could check what day the current week day is with date('w').
Note that Sunday is 0, and Saturday is 6.
And with that information, you can do:
$yesterday = strtotime(date('w') == 0 || date('w') == 6 ? 'last friday' : 'yesterday');
echo date('Y-m-d', $yesterday);
Basically, if it's a weekend, you ask for the last friday, otherwise, you ask PHP for yesterday.
If you don't like it in one line:
$w = date('w');
if($w == 0 || $w == 6)
$yesterday = strtotime('last friday');
else
$yesterday = strtotime('yesterday');
$yesterday = date('Y-m-d', $yesterday);
In our case today, Tuesday, June 14th 2016, it will return 2016-06-13.
For more information about the date function: http://php.net/manual/en/function.date.php

Get previous and next monday date by given date [duplicate]

This question already has answers here:
Get the date of next monday, tuesday, etc
(10 answers)
Closed 11 months ago.
How we can get previous week monday date and next week monday date by provided date.Eample: if $date = '2015-04-08' (y-m-d format).
then function returns, previous monday date = 2015-03-30
and next monday date = 2015-04-13
echo "Next Monday:". date('Y-m-d', strtotime('next monday', strtotime($givenDate)));
echo "Previous Monday:". date('Y-m-d', strtotime('previous monday', strtotime($givenDate)));
you can use
date('Y-m-d', strtotime('last week Monday'));
date('Y-m-d', strtotime('next week Monday'));
or whatever you want http://php.net/manual/en/datetime.formats.relative.php
You can use this:
$nextMonday = new \DateTime('2015-04-08 next Monday');
$previousMonday = new \DateTime('2015-04-08 Monday ago');

Next tuesday, not AFTER this tuesday

How do I (in an elegant way) get strtotime to select the first upcoming tuesday, and use today if today is tuesday?
I first used
strtotime('next Tuesday');
Then the clock passed midnight monday and strtotime began targeting the NEXT tuesday
strtotime('next Tuesday', strtotime('tomorrow'))
does not seem to change anything (still targets the tuesday next week)
strtotime('this tuesday')
works today, but which tuesday will it target tomorrow?
strtotime("this tuesday") is what you're looking for
//given current datetime of Tue Jul 8 03:40:27
print date('Y-m-d',strtotime('this tuesday')); //2014-07-08
print date('Y-m-d',strtotime('this monday')); //2014-07-14
print date('Y-m-d',strtotime('this wednesday')); //2014-07-09
please review this code
<?php
$day=date('D');
if($day=='r'){
$date=date('Y-m-d');
}else{
$date=date('Y-m-d',strtotime('next tuesday'));
}
echo $date;
?>
One way is to compare this tuesday to today, if it is lower than today so next tuesday is next week :
$today = new DateTime("today");
$tuesday = new DateTime("this tuesday");
if($today > $tuesday){
$tuesday->modify("next tuesday");
}
you can avoid using statement.
<?php
$today = date('D');
$Tuesday= strtotime("Monday"); // change To Monday to test
$secondTuesday=strtotime("next Tuesday",$Tuesday);
echo $today === $Tuesday? date('d.m.Y',$secondTuesday):$today ;
?>

How to check that day is last day of month in php?

My previous question: how-to-find-current-day-no-in-month-in-php
Now I face another problem. Suppose I know that this monday is the 4th monday of the current month. Now how to check that this monday is also the last monday of the current month?
exa:1
date: 27-01-2014
Using the code below, I get the name and number of a day:
day: monday
day no :4th
This is the 4th monday of january and it is also the last monday of january. How to check that?
exa:2
date: 20-01-2014
day: monday
day no :3rd
Here this monday is 3rd monday of january but not last monday of january.
So, in short, when I got a day number then how to check whether that the day number is last or not?
code:
$t=date('d-m-Y');
$dayName = strtolower(date("D",strtotime($t)));
$dayNum = strtolower(date("d",strtotime($t)));
$dayno = floor(($dayNum - 1) / 7) + 1
The quickest way to check if today is the last of the month I would suggest the following
if(date('t') == date('d')){
echo 'Last day of the month.';
}
This compares the current day with the number of days of the current month.
Just add 7 days (you still have a monday) to the date you have, and check if it's in the same month.
The complete solution is as follow. Thanks to #Oliver M Grech and #Nanne.
$t=date('d-m-Y');
$day = strtolower(date("D",strtotime($t)));
$month = strtolower(date("m",strtotime($t)));
$dayNum = strtolower(date("d",strtotime($t)));
$weekno = floor(($dayNum - 1) / 7) + 1;
if($weekno=="4" or $weekno=="5")
{
$Date = date("d-m-Y");
$new_month = date('m', strtotime($Date. ' + 7 days'));
if($new_month != $month)
{
echo "This ".$day." is last day of month." ;
}
}

Categories