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.
Related
EDIT: Two minuses in under a minute. I thought this was a tiny but interesting query.
ORIGINAL QUERY: I am generating a five week calender into which I will pour info (the day and date and other stuff).
I want the top left cell to be the "current" Sunday.
For example if today is Weds 12th then I need to find Sun 9th as the start for the run. Then I just do a $var = strtotime("+1 day", $var) for the next 34 slots.
My problem is doing this neatly if today is Sunday.
At present I have:
date_default_timezone_set("Pacific/Honolulu");
$day_now = time();
$current_day= date ("D", $day_now);
if ($current_day == "Sun")
{$day_now = strtotime("+1 day", $day_now);}
$day_now = strtotime("last sunday");
//do stuff/
I just wondered if there was a more "tidy" way of doing this.
I tried "this sunday, last sunday, sunday this week" but could find nothing that would pick today as the Sunday and ALSO work for the rest of the week.
Just curious if anyone has found a form of words that works for this with strtotime.
OK strtotime has NO one phrase solution to the current query ('this sunday" for the whole week). (I would call it a bug!)
The linked discussions
Strange behaviour of strtotime() when using relative dates ('this week')
and
Computing relative dates in php using strtotime()
offer solutions.
I like my current solution - while not elegant it is very readable and I know that if I come back to it in a couple of years I will understand what is going on.
Hopefully "this sunday" will be fixed someday instrtotime.
Thanks for the input.
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...).
I am experiencing a rather strange problem using PHP 5.3's date diff function to calculate the difference in days between two dates. Below is my code:
$currentDate = new DateTime(); // (today's date is 2012-1-27)
$startDate = new DateTime('2012-04-01');
$diff = $startDate->diff($currentDate);
$daysBefore = $diff->d;
echo $daysBefore;
The above code displays 4 as the value of the $daysBefore variable.
Why is PHP displaying a difference of 4 days between the dates 27th Jan 2012 and 1st April 2012, when clearly there are many more days between these dates.
Am I doing something wrong?
DateInterval::$d is the days part of the interval, not the total number of days of the difference. For that, you want DateInterval::$days, so:
$daysBefore = $diff->days;
When creating a DateInterval through the DateTime::diff method, it populates not just days, but hours, minutes, seconds, months and even years in the single character properties. You're checking single-character d for days, which will be the days left over once years and months are calculated.
Try looking at the days property, which only actually gets populated when you use diff.
Behavior here is wildly inconsistent. Check out the DateInterval::format manual page for some interesting information about what happens when you create a DateInterval through various means.
The d property is the number of days as in "3 months, 4 days". If you want the total number of days, use the days property.
4 days, and a couple months...
Use $diff->days for total number of days.
http://www.php.net/manual/en/class.dateinterval.php
Is there a simple way of, given a month and a year, establishing:
How many days there are in that month (factoring in leap years) Done
What day of the week the first day fall upon?
See http://php.net/manual/en/function.cal-days-in-month.php
$num = cal_days_in_month(CAL_GREGORIAN, 8, 2003); // 31
and weekdays:
$weekday = date("l", mktime(0,0,0,$month,$day,$year));
$print ($weekday);
The latter is not very efficient but seems nicer than using getdate:
$my_t=getdate(date("U"));
print("$my_t[weekday], $my_t[month] $my_t[mday], $my_t[year]");
Output like
Wednesday, September 29, 2011
You may find the answer to your questions with all needed variables and calculations by going to wikipedia. http://en.wikipedia.org/wiki/Calculating_the_day_of_the_week
Take a look at the date function, particularly date('t') for the number of days in the month (ie the month given in time()) and date('t',$epoch) for the number of days of the month represented by the timestamp $epoch (which, of course, is given in epoch).
And for the day of the week, there's date('l',$epoch) (where the first argument is a lower-case 'L').