Next tuesday, not AFTER this tuesday - php

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 ;
?>

Related

Show date of next thursday [duplicate]

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');

PHP get the current day and test if it a sunday or saturday

I want to get the current day from a date, and if it's a Firday, I want to set the date for next Monday.
Any idea please to resolve it
Try this
$date = "11-3-2016";
echo date('w', strtotime($date)); //5
echo date('l', strtotime($date));//Friday

PHP date next tuesday 1pm

$tuesday_slot = strtotime('next tuesday');
Above code give me unix time for next tuesday.
How do I get the unix time for e.g next tuesday, 1pm.
Thanks!
Easy :
$tuesday_slot = strtotime('next tuesday');
$tuesday_slot = strtotime('+ 13 hours',$tuesday_slot);
echo date("r",$tuesday_slot);
or
$tuesday_slot = strtotime('next tuesday 1pm');
echo date("r",$tuesday_slot);

Get date of this week thursday OR next week thursday in php

I need to get the DATE of this week's Thursday (or any other day) OR next week's Thursday (or any other day)
The scenario is that there are 3 tours happening every week.. I need to find out next tour date
If today is tuesday and date is 21/01/2014 and tours happen on tuesday, friday and sunday.. then I need to find out next tour date which will be on friday on friday
Similarly, if today is Friday, I'll need to find out date on next week's tuesday.
So far, I tried using strtotime("next tuesday") but it doesn't seem to work well
echo strtotime("next Thursday");
Example straight from PHP.net, should work. And for the Thursday after that one you can do
$nextThursday= strtotime("next Thursday");
$secondThursday=strtotime("next Thursday",$nextThursday); // And so on
Demo
Try this,
<?php
$next_thursday = strtotime("next Thursday");
$this_thurday = strtotime('thursday this week');
if($next_thursday==$this_thurday)
{
$numberOfWeeks = 1;
$next_thursday = $next_thursday + ($numberOfWeeks * 60 * 60 * 24 * 7);
}
echo date("Y/m/d", $this_thurday);
echo date("Y/m/d", $next_thursday);
?>
I was looking for something like this and found the following in a comment here from deceze:
(new DateTime('now'))->modify('+5 days')
I used this in the follow way:
$date = DateTime('15 apr 15');
$date->modify('next thursday');
var_dump($date);
This worked for me so I am putting this out there as another possibility for those looking.

PHP find first Thursday in a month

I'm working on a time management program and was wondering how I would find the first Thursday in a certain month. Does PHP have any functions to aid with this? strtotime doesn't want to format some strings properly.
If i use strtotime("first thursday december 2011") it returns 1323302400 which is actually the second thursday (8th december).
If i use strtotime("first thursday december 2011 - 1 week") it returns 1322092800 which is 24th November.
Can someone lend a hand please!
Read PHP Bug #53539. The fix is to use of: strtotime("first thursday of december 2011")
Note that many DateTime bugs were fixed in PHP 5.3. If you are running < 5.3 you should really upgrade.
strtotime('next thursday', strtotime('last day of november'));
or even cleaner:
strtotime('thursday', strtotime('1 december'));
There is a similar function witten here, for the first Monday of the month. Does that help?
strtotime('first friday', strtotime('2023-03-00'))
function firstThursdayOfMonth() {
$today = getdate();
$firstdayofmonth = date('Y-m') . '-01 00:00:01';
if($today !=$firstdayofmonth) {
echo $firstdayofmonth;
}
else if($firstdayofmonth != date('Y-m-d[3]'.'-01 00:00:01') ) {
echo $today;
}
else {
echo $today;
}
}
echo firstThursdayOfMonth();

Categories