I'm using Zend_Date to create dates.
How can I get date of the nearest further day of week, Monday, for example. I need to calculate it not only from now, but from any date.
Example.
January, 31, Thursday.
I need to calculate date of the nearest Monday.
Result of function call should be February, 4.
i dont know in zend but surely you can use date('Y-m-d',strtotime('next monday')); in php
Related
is there any parameter or configuratión to provide to a DateTime object to not consider week number in iso but taking the first day of january as the first week of the year?
As Datetime works with an ISO rule, the first week of the year is the week with the first thursday.
I need to make some operations with datetimes but i need it to consider the first day of January as the first week of the year, even if its a Sunday.
Is this possible? I tried everything.
Thank you.
Don’t think that is possible with the native DateTime, no - that only follows ISO 8601 rules. You’d have to handle that yourself, by taking the difference between Jan 1st vs Jan 4th as start of the first week into account in all places where you operate with those values.
You should consider using a date library such as Carbon - with the proper locale set, that should be able to do what you want.
https://carbon.nesbot.com/docs/#api-week:
Week methods follow the rules of the current locale (for example with en_US, the default locale, the first day of the week is Sunday, and the first week of the year is the one that contains January 1st)
There is no option to set the start weekday of first week.
You can only use following format for week number of the day:
%U - week number of the current year as a decimal number, starting with the first Sunday as the first day of the first week
%V - The ISO 8601:1988 week number of the current year as a decimal number, range 01 to 53, where week 1 is the first week that has at least 4 days in the current year, and with Monday as the first day of the week. (Use %G or %g for the year component that corresponds to the week number for the specified timestamp.)
%W - week number of the current year as a decimal number, starting with the first Monday as the first day of the first week
So if you want to calculate week number assuming 1st day of year as first week of year, then you have to make your own function to add 1 if 1st Jan is not Thursday or before.
Taken from my own answer and adjusted for dateTime and the year starting with week 1.
function get_week($date){
$w=(int)$date->format('W');
$m=(int)$date->format('n');
return $w==1?($m==12?53:1):($w>=51?($m==1?1:$w):$w);
}
2016-01-01 is officially week 53, but the function returns 1. Week 1 now has 10 days, till 2016-01-01. Week 2 starts at 2016-01-11
Function below provides week number 01-53 starting 1st of January. Note: in such way the first and the last week of the year could be less than 7 days.
function getWeekNumberStartingJan1(DateTime $date)
{
if ((clone $date)->modify('1 january')->format('W') !== '01') {
if ($date->format('o') !== $date->format('Y')) {
return '01';
}
return sprintf('%02d', (int)$date->format('W') + 1);
}
return $date->format('W'); //year started from Monday
}
It is giving correct date but not the correct day value.Why..?
What I want day is the date for monday for the current week which can be generated on any day of the week. so what I did was, I'm taking the today's day and comparing with (Mon,Tue.... Sun) and respectively creating a timestamp using
You can use date("Y-m-d",strtotime("monday")); to find out next Monday.
To find Monday from any day of the week you can try this-
$date="2018-08-01"; //any day of the week
$day=date("N",strtotime($date));
if($day>1){
$date=date("Y-m-d",strtotime("$date -1 week"));
}
echo date("Y-m-d",strtotime("$date monday"));
Didn't sure what you really want but if you want current day date simply use getdate() function
I need to find the date number for the first day of the current week. For example if the date is 05/06.2015 I want to get as a result 29/05/2015 where Monday is the firs day of the week. I'm not very familiar with PHP date functions because I'm new at it. Can someone show me how to find it? Thanks!
strtotime is a powerful function.
echo date("j/n/Y", strtotime('monday this week'));
If you have the day of the year. How can you convert that to day of month and month? For example: The day "144" should be converted to 26th of May. I guess I also have to add the actual year to account for leap years. But I haven't found anything at all.
For example the function mktime() exepects the month, year and day of month.
Anybody some suggestions?
The most reliable and convenient way is to use the DateTime object. You can use DateTime::createFromFormat() static method to create it based on day in the current year:
$date = DateTime::createFromFormat('z', '144');
And because you know have DateTime object in the $date variable, you can perform literally any task you want to. To output the contained date, simply call:
echo $date->format('j. n. Y');
It will print out 24. 5. 2012, because it's leap year and because it indexes days starting from zero (just like array indices).
strtotime("January 1st +".($days-1)." days");
This will return a timestamp corresponding to the specified day of the year.
I have a string like 09-10 which is representative of mm-dd. I need it in a format something like Monday 10th September? The problem is that I do not have a year and I can't have an array containing months and days because I would like to know the day of the week (Mon, Tue, Wed etc.)
Any idea how to do this in PHP, preferably using date() to format the date?
Note: this is not in MySQL...
You can't get the day (Monday, Tuesday etc) without knowing the year.
You can use date('jS F', strtotime('2012-09-10')); to get the day of the month and month, just shove any old year in there. I'd make sure to use a leap year year though to make sure you catch those pesky feb dates properly.
Example: http://codepad.org/JfUeTQlH
So, like this:
$d_m = '09-10';
$my_date = date('jS F', strtotime('2012-'.$d_m));
As it is clear Every 1st day of year is not Sunday, it happens after regular interval So just saying a date without year is not clear about day(Monday,Tuesday...). it will give you number of day in that year i.e. out of 365days. so if you are working within a year its OK, but if it goes beyond it You wont be able to get the day(Monday,Tuesday...).