PHP stringtotime function first weekday - php

echo date("d-M-Y", strtotime("first monday 2019-07"));
Shouldn't the output of this line be 01-Jul-2019? Thats the first monday in July but the output is: 08-Jul-2019.
What is the error im making?

Try
first monday of 2019-07
Instead of first monday 2019-07.

Use,
echo date("d-M-Y", strtotime("first monday of 2019-07"));
You can find here php instructions to parsing date as natural languages.
http://php.net/manual/en/datetime.formats.relative.php

" first monday 2019-07 " returns the date of the first monday of july excluding the start date (in this case 1st july 2019). Where as "first monday of 2019-07" returns the date of the first monday of july including the start date.

Related

PHP DateTime::format() returns wrong date in 1995

I've got a timestamp like 812671200. https://timestampconvert.net/ says it is 10/03/1995 00:00:00. Fine.
But if I want to format it in PHP like this:
$date = new DateTime();
$date->setTimestamp(812671200);
$date->setTimezone(new DateTimeZone('Europe/Berlin')); // my timezone
echo $date->format('m/d/Y H:i:s');
It says: 10/02/1995 23:00:00Using "UTC" as timezone returns: 10/02/1995 22:00:00
I read on Wikipedia, that in 1996 DST changed to last Sunday in October, so I tried different timestamps like last Saturday, Sunday of September 1995, they formatted fine, but Monday after last Sunday of September 1995 did not format right.
Wikipedia - Daylight saving time by country
1981–1995 last Sunday of March to last Sunday of September.
Since 1996 last Sunday of March to last Sunday of October.
Is this a problem with PHP or am I doing something wrong?
Help is very appreciated. Thanks!

how to find a date from the value of week of the month in laravel..?

I need to find first, second, etc, week in a month.
In that week, i need to find a Monday/Tuesday or else.
I'm using laravel framework and php,
Now, i'm going to find Monday of first week of this September month and
my code is,
date('Y-m-d', strtotime('+0 week, Monday', strtotime('first day of September 2016')));
This is correct way or not..?
Here, the answer is no date or 5-9-2016 ..?
I would higly recommend you to read about Carbon, it comes by default with Laravel and is what is used internaly to deal with dates. Also is really handy and easy to use.
It's much simpler than you think using strtotime. I believe the last few are what you're after. You'll have to be careful though, some weeks in partiular months may not have a particular day. For instance the 1st week of November 16 does not have a Monday.
<?php
echo date("Y-m-d",strtotime('first monday September 2016'));
// 2016-09-05 (first Monday of september '16)
echo date("Y-m-d",strtotime('Tuesday +1 week December 2016'));
// 2016-12-06 (first Tuesday of December '16)
echo date("Y-m-d",strtotime('Friday November 2016'));
// 2016-11-04 (first Friday of November '16')
echo date("Y-m-d",strtotime('fourth Monday September 2016'));
// 2016-09-05 (first Monday of september '16)
echo date("Y-m-d",strtotime('Tuesday December 2016'));
// 2016-12-06 (first Tuesday of December '16)
echo date("Y-m-d",strtotime('third Friday November 2016'));
// 2016-11-04 (first Friday of November '16')
echo date("Y-m-d",strtotime('Tuesday +1 week December 2016'));
// 2016-12-06 (Tuesday of the second week of December '16)
echo date("Y-m-d",strtotime('Friday +2 week November 2016'));
// 2016-11-04 (Friday of the third week of November '16')
?>
Without the '+n week' in the last 2 examples, it's going to return the first matching day in the first week. Adding +1 week makes it the second week it chooses the day from. Adding +2 week makes it the third week it chooses from, etc.
But like I mentioned above, it's not foolproof. For instance:
echo date("Y-m-d",strtotime('Friday +4 week November 2016'))
// 2016-12-02 Although there are 5 calendar weeks in November '16, the 5th
// week does not have a Friday. So you're getting December's Friday, because
// you're simply adding weeks to a day.

getting the numeric value of "this Monday" - php

I'm trying to display a numeric value of the current week,
in a calendar-like feature that I have on a website I'm working on.
So, the function that I came up with in the end, working, is...
echo date("d", strtotime("this Tuesday"));
All goes well for most of the days,
except that, for example, today is Tuesday, the 25th, all week dates display well
since Tuesday to Sunday (25 - 30),
but for Monday it will say 01, because I think it thinks that "this Monday" is the 01 of December, not yesterday. This Monday usually should be the Monday of this week, yesterday (24), but the function when you apply "this Monday" will display the next Monday, because today it's already Tuesday, and yesterday is out of the scope ?
But this is weird, because i'm reffering to "this Monday", Monday of this week. It displays the next Monday. Is that a bug in the date function ?
How do I safely get the values of the days of the week otherwise?
thanks!
strtotime("Monday this week");
From the manual:
reltext space 'week' | Handles the special format "weekday + last/this/next week". | "Monday next week"
Try it with "last Monday" and "next Monday" keywords.
For Example:
echo date("d", strtotime("last Monday"));
another example...
echo date("d", strtotime("next Monday"));

PHP strtotime() is not returning the correct month

Being that the current month/year is January 2012, why does the following code return December 2011 and not November 2011?
echo date("F Y", strtotime("-2 months"));
This is on PHP 5.3.0 if it makes a difference.
To get what you are looking for you can use this rather verbose version instead:
echo date("F Y", strtotime("first day of this month - 2 months"));
The problem with your original version is described in detail here: http://derickrethans.nl/obtaining-the-next-month-in-php.html. Quoted below:
Over and over again PHP users complain that next month in PHP's
date-string parser doesn't go to the next month, but instead skips to
the one after next month; like in the following example:
<?php
$d = new DateTime( '2010-01-31' );
$d->modify( 'next month' );
echo $d->format( 'F' ), "\n";
?>
The output of the little script will be March. March obviously doesn't
follow January as February is in between. However, the current
behavior is correct. The following happens internally:
next month increases the month number (originally 1) by one. This
makes the date 2010-02-31. The second month (February) only has 28
days in 2010, so PHP auto-corrects this by just continuing to count
days from February 1st. You then end up at March 3rd. The formatting
strips off the year and day, resulting in the output March. This can
easily be seen when echoing the date with a full date format, which
will output March 3rd, 2010:
This is for adding months, but the same applies in reverse when subtracting months; there was no November 31st, so the strtotime method "corrects" it into December 1st.

work out the date of the fourth saturday in the current month

Bit stuck about how to go about this one. Given the current month, I need to to return the date of the fourth saturday of each month.
e.g. This month would be Feb 20th, next would be March 27th.
Thanks
I'm not a PHP coder, however, it seems strtotime is what you're after.
You can use strtotime("fourth Saturday") and it will return the 4th saturday.
Check out the strtotime docs.
EDIT:
Just to make the answer complete, thanks to Tom and Paul Dixon
date('dS F',strtotime('Fourth Saturday '.date('F o')));
You can use strtotime to find "next saturday" based on a starting date. If that starting date is the day before the earliest possible preceding day (21st) we get the answer...
//required year/month
$yyyymm="2009-01";
//find next saturday after earliest possible date
$t=strtotime("next saturday", strtotime("{$yyyymm}-21"));
//here you go!
echo "4th saturday of $yyyymm is ".strftime("%Y-%m-%d",$t)."\n";
Earliest possible 4th repeat of a day in any month is the 22nd (1,8,15,22), last possible 4th repeat is 28th (7,14,21,28).
EDIT: Although it's not clear in the documentation, you can request the "fourth saturday" too - use the zeroth day of the month as the basis:
$t=strtotime("fourth saturday", strtotime("{$yyyymm}-00"));
or omit the basis time and specify the month and year directly:
$t=strtotime("fourth saturday feb 2009");
Tip of the hat to Robin "I'm not a PHP coder" Day for spotting that :)
The earliest date for the fourth Saturday is the 22nd of the month. So look at the 22nd, see what day of the week it is, if it's not Saturday, add one day to the date, and check again, until you find a match (maximum you would have to check is 6 days).
Find the first Saturday of the month, and then add three weeks to that.
If you don't know when the first Saturday is (or, rather, don't know specifically a date corresponding with a day name), you might want to look at the Doomsday algorithm, which I conveniently looked at for another post with a somewhat similar issue.
function fourth_saturday($year, $month)
{
$info = localtime(mktime(0, 0, 0, $month , 1, $year));
return 28 - $info[6];
}
in PHP rather than pseudo code (think requires 5.2)
$date = getdate();
$date-> setDate($date->format('Y'), $date->format('Y'), '1'); // 1st of month.
while ($date->format('w' != 6)
$date->modify("+1 day");
$date->modify("+21 day"); // date is now on the fourth saturday

Categories