this week in PHP 5.3 - php

I try to find an answer but without result
the problem is:
in sunday function return date for Monday next week
$date = new DateTime();
$date->setTimezone(new DateTimeZone('Europe/Moscow'));
$date->setTimestamp(strtotime('Monday this week'));
echo $date->format("d.m.Y");
but in other days (except Sunday) its return correct value of Monday.
I ever set locale manualy , which has a monday - the first day of week, but PHP "think" the Sunday is still firs day. is it bug ?? or i do some wrong ?

There seem to be known idiosyncrasies with strtotime. It's mentioned in the comments:
http://www.php.net/manual/en/datetime.formats.relative.php#108317
They don't mention different locales specifically but it would be extremely unsurprising if strtotime() does not correctly behave for next/this week under different locales.
Try this Monday or next Monday and see if one of those gets what you want.

Related

how to get week +4 in this day with carbon

I have a problem here I want to get the +4 week date from the current date using carbon, the +4 week plan will be dynamic depending on the input entered by the user, how do I make it, I've tried using this code but it's time to step back
$dt = Carbon::now();
dd($dt->week(4)->format('Y-m-d'));
Check Carbon docs, you may use addWeeks():
$dt = Carbon::now();
dd($dt->addWeeks(4)->format('Y-m-d'));
The week() method you've used, sets the week number using given first day of week and first day of year included in the first week.
I am not sure to understand your question, but I guess you just have to use :
$dt = Carbon::now();
$dt->addWeeks(4);
dd($dt->format('Y-m-d');
You don't need carbon for such simple tasks.
With DateTime
echo date_create('+4 weeks')->format("Y-m-d");
or with date and strtotime
echo date("Y-m-d",strtotime('+4 weeks'));

PHP strtotime returning wrong result when looking for a "first saturday" of a given month [duplicate]

I'm working on a PHP function which calculates holidays:
function holidays($country = 1, $timespan_start = 0, $timespan_end = 0)
The holidays are returned as timestamps in an array.
Since I have to calculate dates like the first Monday of February, I tried strtotime("first monday february $year") and I've discovered that this does not work for 2010, since 02/01/2010 is a Monday - I get February 8th instead.
This bug is actually mentioned in the change log:
In PHP 5 prior to 5.2.7, requesting a given occurrence of a given weekday in a month where that weekday was the first day of the month would incorrectly add one week to the returned timestamp. This has been corrected in 5.2.7 and later versions.
But I'm using PHP 5.3.8.
Why am I experiencing this error?
Looks like you are just missing an "of":
echo date('Y-m-d', strtotime('first monday of february 2010'));
will give the expected result. See the PHP Manual on Relative dates for the various input formats.
Depending on your version of PHP the 'of' statement may or may not work. As another solution try:
echo date('Y-m-d',strtotime('monday February 2010'));
will return the first monday of February 2010. Works for all days as well.

Get day of the next week by monday with php

i want to get the day of the next week with php..i need something like this
If today is monday, i want to display the days (es 18-19-20) of the next week, not of this week..the week start on monday...but it will work also if today is sunday..always the days of the next week..i can't found anything because i tried to use jddayofweek but i don't understand it maybe..i haven't found an italian guide..
I have a table which display the days in stringn form..so sunday monday etc, fir my problem i tried date() function but if i use +1 on the day i will have problem with february for example
Not sure exactly what you're doing, but maybe something like:
echo date("d", strtotime("monday next week"))
This will give you next Monday:
$date = date_create()->modify('monday next week');
echo date_format($date, "d");

Why is strtotime one week off?

strtotime("third Saturday October 2011")
Should be 10/15/2011. However, it's coming up at 10/22/2011. I assume this is because October 2011 starts on a Saturday and PHP is looking at the first full week. Since 10/1/2011 is a Saturday not a full week it ignores it.
Some research suggested putting "of" between the day of the week and the month should fix it but that doesn't work.
Any suggestions on why this is happening and what I can do to correct it?
This is a documented flaw in PHP <5.2.7 (see strtotime):
In PHP 5 prior to 5.2.7, requesting a given occurrence of a given
weekday in a month where that weekday was the first day of the month
would incorrectly add one week to the returned timestamp. This has
been corrected in 5.2.7 and later versions.
You'll need to upgrade PHP, or use a work around like Jonathan Kuhn suggests.
I was getting the same results as you on php 5.2.6. This works for me although not ideal.
echo date('Y-m-d', strtotime('Saturday October 2011 +2 weeks'));
<?php echo date('Y-m-d', strtotime('third Saturday of October 2011')); ?>
Works fine for me. Output is 2011-10-15.

PHP strtotime - get next closest "second sunday of the month"

I am trying to get the timestamp of the next "second sunday of the month", regardless it is in the current month or in the next month.
The requirements are to use the strtotime function, only once, and without second parameter, nothing more (these constraints come from legacy code).
Basically the PHP code to get that must be only:
strtotime('_a_string_here_');
And the string must be a generic string. It can't contain something like 'feb 2011'.
Basically I can just drop a static string as first argument of the strtotime function.
I have tried all of these but they don't give me what I want (now the date is Thu 2011-02-24):
strtotime('second sunday') // it returns '2011-03-06 00:00:00'
strtotime('second sunday of the month ') // 1970-01-01 00:00:00
strtotime('+0 month second sun') // it returns '2011-03-06 00:00:00'
Thanks,
Dan
Try this one:
strtotime('+2 week Sunday')
New Answer:
I think you're expecting strtotime to be too smart. By not providing a month in the string, or a second parameter, it can only assume 'current time'.
There is no word support for conditionals like 'second sunday of current month if not past, else second sunday of next month'.
IMO adding stuff like that to strtotime would make it too complex to be of any use in normal operations. The correct solution would most likely be to make two calls to strtotime (the first to see if the current month has past, the second only if we need the next month).
I don't know why your constraints exist, but you should probably start figuring out how you can change the constraints.
None of the suggestions worked for me.
Here's what did though:
date('m/d/Y', strtotime('Second Sunday Of March 2015'));
For the discussion about the "next month problem" check http://derickrethans.nl/obtaining-the-next-month-in-php.html
// second sun next month
strtotime('+0 month +2 week sun');
To solve this you just need to normalize to the first of the month in question. So say you want the second Sunday of March for daylight savings.
date('m/d/Y',strtotime('03/01/2011 second sunday'));
or more generically
date('m/d/Y',strtotime('03/01'.date('Y').' second sunday'));
Hope this helps.
Have you tried.
strtotime('second sunday of this month');

Categories