PHP find first Thursday in a month - php

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

Related

How to set the "first day of the week" to Thursday in PHP

I want to set the first day of the week to Thursday (not Sunday or Monday), because it's the company's cut-off date.
I already have a code to determine the current week number of a date but it starts in Sunday or Monday.
How to modify these to my preference?
function findweek($date) {
$monthstart=date("N",strtotime(date("n/l/Y",strtotime($date))));
$newdate=(date("j",strtotime($date))+$monthstart)/7;
$ddate=floor($newdate);
if($ddate != $date) {
$ddate++;
}
return $ddate;
}
http://php.net/manual/en/datetime.formats.relative.php says that as of PHP version 5.6.23, 7.0.8 "Weeks always start on monday. Formerly, sunday would also be considered to start a week." That said, is your problem that the number of weeks returned might be incorrect depending on whether today falls on or before Thursday of the current week? Maybe try something like this:
$date = new DateTime();
$week = intval($date->format('W'));
$day = intval($date->format('N'));
echo $day < 4 ? $week-1 : $week;
If subtracting 1 isn't the answer you could play around with addition/subtraction, comparing the result with the actual answer you know to be true until you get the right formula. Hope this helps!
This should work.
function findweek($date, $type = "l") {
$time = strtotime($date);
return date($type, mktime(0, 0, 0, date("m", $time) , date("d", $time)-date("d", $time)+1, date("Y", $time)));
}
echo findweek('2015-09-16');

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

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: strtotime("12/31/2004 +6 month")); not returning the last day of June

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.

how to get the date of last week's (tuesday or any other day) in php?

I think its possible but i cant come up with the right algorithm for it.
What i wanted to do was:
If today is monday feb 2 2009, how would i know the date of last week's tuesday? Using that same code 2 days after, i would find the same date of last week's tuesday with the current date being wednesday, feb 4 2009.
Most of these answers are either too much, or technically incorrect because "last Tuesday" doesn't necessarily mean the Tuesday from last week, it just means the previous Tuesday, which could be within the same week of "now".
The correct answer is:
strtotime('tuesday last week')
I know there is an accepted answer already, but imho it does not meet the second requirement that was asked for. In the above case, strtotime would yield yesterday if used on a wednesday. So, just to be exact you would still need to check for this:
$tuesday = strtotime('last Tuesday');
// check if we need to go back in time one more week
$tuesday = date('W', $tuesday)==date('W') ? $tuesday-7*86400 : $tuesday;
As davil pointed out in his comment, this was kind of a quick-shot of mine. The above calculation will be off by one once a year due to daylight saving time. The good-enough solution would be:
$tuesday = date('W', $tuesday)==date('W') ? $tuesday-7*86400+7200 : $tuesday;
If you need the time to be 0:00h, you'll need some extra effort of course.
PHP actually makes this really easy:
echo strtotime('last Tuesday');
See the strtotime documentation.
Working solution:
$z = date("Y-m-d", strtotime("last Saturday"));
$z = (date('W', strtotime($z)) == date('W')) ? (strtotime($z)-7*86400+7200) : strtotime($z);
print date("Y-m-d", $z);
you forgot strtotime for second argument of date('W', $tuesday)
hmm.
convert $tuesday to timestamp before "$tuesday-7*86400+7200"
mde.
// test: find last date for each day of the week
foreach (array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun') as $day) {
print $day . " => " . date('m/d/Y', last_dayofweek($day)) . "\n";
}
function last_dayofweek($day)
{
// return timestamp of last Monday...Friday
// will return today if today is the requested weekday
$day = strtolower(substr($day, 0, 3));
if (strtolower(date('D')) == $day)
return strtotime("today");
else
return strtotime("last {$day}");
}
<?php
$currentDay = date('D');
echo "Today-".$today = date("Y-m-d");
echo "Yesterday-".$yesterday = date("Y-m-d",strtotime('yesterday'));
echo "Same day last week-".$same_day_last_week = date("Y-m-d",strtotime('last '.$currentDay));
?>
Do not use manual calculation, use DateTime object instead. It has proper implementation, takes into account leap years, yeap seconds, etc.
$today = new \DateTime();
$today->modify('tuesday last week');
The modify method modifies the date relative to it's state, so if you set the date to a different date, it calculates it relative to it.
$date = new \DateTime('2020-01-01');
echo $date->format('Y-m-d'); // 2020-01-01
$date->modify('tuesday last week');
echo $date->format('Y-m-d'); // 2019-12-24

Categories