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.
Related
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
So I'm just making a basic calendar for each month, just to play with the date function in PHP. I use something very simple code that I was thinking of throwing into a loop and populate some cells in a table:
public function getDayDate(){
$month = "January";
$day="1";
$year="2014";
$theW = "$month $day $year";
//First day of the week on a month
echo date("D", strtotime($theW));
//# of days in a month
echo date("t", strtotime($theW));
}
But it came to my mind about leap year and all other kinds of calendar events that may effect the number of days in a month. And i was wondering if this basic setup automatically factors these things in with the data here. Cause I figured I can have start on a particular cell like Wednesday and loop it 28-31 times to add the day to each cell until it completes.
Is this wrong? I tried searching for about a day, and most of the questions are more specific for finding the leap year and/or event, instead of it automatically just giving the end result, which is the number of days in the month and what day of the week it starts on.
I appreciate your help!
Yes, PHP's DateTime class does. You can even check if it is a leap year with the L formatter.
$date = date_create();
$isLeapYear = $date->format('L');
var_dump($isLeapYear);
You can easily test it. Calculate the diff between Feb 27th and March 2nd for example in a leap year and in a non leap year and you will see that they are different.
I recommend you use DateTime::createFromFormat for transforming your string into a date.
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
I'm trying to jump from month to month, starting from a specific timestamp, but when I get jump from August, September always gets skipped. Starting From August 31 (1346389200) and jumping 1 month:
strtotime('+1 month', 1346389200);
yields 1349067600 - which is October 1st.
I've read all about strtotime making mistakes if it doesn't have a starting date to calculate from, but what could the issue be with this?
Thanks
One month after 31 August is 31 September, but, because it not exists, php force the result to 1 Oktober.
So, you should force the current month on 1th day (of course if you want only year and month) :
strtotime('+1 month',strtotime(date("Y-m-1",1346389200)));
but if you use php >5.3 you can use more reliable DateTime class and methods.
You probably shouldn't use timestamps and strtotime for this comparison. You will introduce problems because of things like daylight savings, leap years, etc. Best to use DateTime and DateInterval classes/functions to do this in a more thorough manner.
http://php.net/manual/en/datetime.add.php
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...).