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.
Related
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;
}
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 ;
?>
I expected this functional to return 6/30/2005 instead of 7/1/2005.
print date("m/d/Y", strtotime("12/31/2004 +6 month"));
Similarly, print date("m/d/Y", strtotime("1/31/2011 +1 month")) returns 03/03/2011 while would like it to return 2/28/2011.
Does anyone know if there is a straight forward way to show the last day of the added month?
How about this?
echo date("m/d/Y", strtotime("last day of 12/31/2004 + 6 month")); // 6/30/2005
echo date("m/d/Y", strtotime("last day of 1/31/2011 + 1 month")); // 2/28/2011
Demo
Edit: For your reference, here is a link to the documentation for relative times.
as strtotime continue in to next month if there isn't enoghe days that month,
you can back 6 month and check if its end up on the start date
$date2 = date("Y-m-d", strtotime("{$date} +6 months"));
$date3 = date("Y-m-d", strtotime("{$date2} -6 months"));
if($date3 != $date)
{
$date2 = date("Y-m-t", strtotime("{$date2} -1 months"));
}
(or in your case "m/t/Y")
One simple way is to actually go one month ahead of the day you want and then make the day value zero. Also, mktime() might be easier
$mymonth = 2; // I want the last day of February
echo date('m/d/Y', mktime(0,0,0,$mymonth+1,0,2011));
This should return 2/28/2011.
strtotime does the best it can with conflicting information. Saying
1/31/2011 +1month
would mean advancing to
2/31/2011
but February only has 28 (sometimes 29) days. 2011 isn't a leap year, so the "31st of February" gets normalized to "March 3rd".
The same applies for '12/31/2004 +6month'. That takes you to June 31st, 2005. But June only has 30 days, so the date is normalized to July 1st instead.
im looking for the DateTime modify String for the first day of the year (now 1. January 2011). I tried the following:
<?php
$time = new DateTime();
// works as expected, the first day of the current month
$time->modify('first day of this month');
echo $time->format('c')."\n";
// this doesn't work. I also tried several other ways
$time->modify('first day of january');
echo $time->format('c')."\n";
>
I know there are other ways to retrieve the date, but I search an string for DateTime->modify() no other solution.
You should specify the year too, as you can see in this example:
"first day of January 2008"
from the official doc.
Update: It works on php version >= 5.3.6
On v5.5.6
echo date('Y-m-d', strtotime('first day of January this year'));
Result: 2013-01-01
To get the day of the week for the first of the year
or the first day of the month
<?php
//This is for a given month
$m="May";
// this id for this month
//$m=date('F');
//if you want the day of Sunday instead D use lower case l
echo date('D', strtotime('first day of January this year'));
echo "<br>". date("D", strtotime('first day of'. $m ));
?>
Result Wed For May with D
Result Wednesday with l
This work for me (PHP 5.6 - not tested on older version)... as we talk for DateTime object
//Get current datetime
$now = new DateTime();
$now->modify('first day of January this year');
echo $now->format('Y-m-d');
// Print (current year)-01-01
echo (new DateTime())->modify('first day of January this year')->format('Y-m-d');
A client wants a newsletter to be automatically generated every Monday, showing the schedule for the upcoming week. That's easy:
if(date('N', $time)==1) { /* Stuff */ }
Attach that to a crontab running nightly and I'm good to go.
However, if the newsletter is being generated in the last week of the month, it needs to show the schedule for the upcoming month. How would I determine when the monthly schedule needs to be generated?
date('m') == date('m', strtotime('+1 week'))
If the month a week from the date the report is running is different than the current month, show the report!
if(date('n', $time) !== date('n', $time+518400)){
// Six days from now it will be a new month
}
One way might be to see if next Monday is in a different month.
if (date('n', $time) != date('n', $time + 7*24*60*60)) { ... }
You could be fancier, but this seems consistent with your existing code.
You can use the t date format param to see how many days are in the particular month. Try
if ((date('t', $time) - date('j', $time)) > 6) {
// in the last week of the month
}
I know this a answered but this will help more:
PHP's built-in time functions make this even simple. http://php.net/manual/en/function.strtotime.php
// Get first Friday of next month.
$timestamp = strtotime('first fri of next month');
// Get second to last Friday of the current month.
$timestamp = strtotime('last fri of this month -7 days');
// Format a timestamp as a human-meaningful string.
$formattedDate = date('F j, Y', strtotime('first wed of last month'));