With these variables..
$month1 = 8; // A month in the past
$month2 = date('n'); // Current month
...I need to a loop that outputs this:
August, September, October, November, December, January, February, March
8 is obviously the 8th month, hence August.
I realise the vars are using numeric representation, but the output needs to be a comma separated list of full textual representations of the month names.
Any help would be awesome.
$month1 = 8; // A month in the past
$month2 = date('n') + 12;
for ($i = $month2; $i >= $month1; $i--)
$m[] = date('F', mktime(0, 0, 0, ($i % 12), 10));
$months = implode (",", $m);
print $months;
Related
I have some strange behaviour happening when I run the following on the last day of May (31st). If I change my system time to 30th May, or the last day of say, June (30th), it functions normally.
For some reason though, on the 31st, it will skip the next month (June), and instead replace it with July. So it will output July twice. Here is an example:
31 days in 05, 31 days in 07, 31 days in 07, 31 days in 08,
Code which generated the above
<?php
$datepicker_month_range = 3;
// create an array of dates for a number of months specified by the user. 0 is used for this month
for ($month = 0; $month <= $datepicker_month_range; $month++) {
$dt_dates = new DateTime();
$dt_dates->add(new DateInterval("P{$month}M")); // example, P1M == plus 1 month
$days_in_month = cal_days_in_month(CAL_GREGORIAN, $dt_dates->format('m'), $dt_dates->format('Y'));
echo $days_in_month." days in ".$dt_dates->format('m').", ";
for ($day = 1; $day <= $days_in_month; $day++) {
$date = $dt_dates->format('Y')."-".$dt_dates->format('m')."-".sprintf('%02d', $day); // leading zeros 05-..
$month_days[] = $date;
}
}
//print_r($month_days);
?>
Later on, if print_r($month_days) is run, the complete dates are outputted with July outputted twice like in the previous expression.
What is causing this behaviour?
Thanks.
Ok, after reading the comments, it seems this is a duplicate of PHP DateTime::modify adding and subtracting months
but here is how I got around the problem.
$month_beginning = $dt_dates->format('Y-m-01');
$dt_dates = new DateTime($month_beginning); // rollback the date to the first so we can increment months safely
All together
for ($month = 0; $month <= $datepicker_month_range; $month++) {
$dt_dates = new DateTime();
$month_beginning = $dt_dates->format('Y-m-01');
$dt_dates = new DateTime($month_beginning); // rollback the date to the first so we can increment months safely
$dt_dates->add(new DateInterval("P{$month}M")); // P1M == plus 1 month
$days_in_month = cal_days_in_month(CAL_GREGORIAN, $dt_dates->format('m'), $dt_dates->format('Y'));
//echo $days_in_month." days in ".$dt_dates->format('m').", ";
for ($day = 1; $day <= $days_in_month; $day++) {
$date = $dt_dates->format('Y')."-".$dt_dates->format('m')."-".sprintf('%02d', $day); // leading zeros 05-..
$month_days[] = $date; // holds dates for datepicker month ranges
}
}
Current Date is 29th March 2017
When I subtract 2 months using PHP and I get January
$prevmonth = date('M', strtotime('-2 months'));
echo $prevmonth;
But when I subtract 1 month it gives March
$prevmonth = date('M', strtotime('-1 months'));
echo $prevmonth;
strtotime() uses 30 day months and there are only 28 in days in February (this year) so will not yield a valid date in February. You could use the current day d or j and subtract that which will always put you in the previous month (-29 days):
$prevmonth = date('M', strtotime('-' . date('d') . ' days'));
This will get December from January as well.
As covered in the comments, there's no 29th Feb.
29th Feb becomes 1st March.
You may be better to get the current month number, -1 from it, and then get the textual representation.
$prevMonth = date('n') - 1;
$prevMonthText = date('M', mktime(0, 0, 0, $prevMonth, 10));
Or, you could use DateTime if your PHP version allows (it should).
$prevMonth = date('n') - 1;
$dateObj = DateTime::createFromFormat('!m', $prevMonth);
$monthName = $dateObj->format('M'); // March
The only issue with this, that you might have spotted, is January will never return December. A quick ternary statement will catch that.
$prevMonth = ((date('n') - 1) < 1) ? 12 : date('n') - 1;
Given a Month and a weekday, I need to build a function that can retrieve the day number of all Mondays, Tuesdays, Wednesdays, Thursdays and Fridays.
Let's say I give the function this month, September 2012 and weekday number 1. The function should retrieve all the Mondays in September 2012 which are: 3, 10, 17 and 24
Please note that to me weekday number 1 is Monday, number 2 Tuesday, 3 is Wednesday, 4 Thursday and 5 Friday.
So far I've have done: getting the first day of the week given today's date (I post the function below). But I don't know how to follow from here in a simple way, I've been many hours on it and I suspect there's a better way to do it. Can you please show me how?
function getFirstDayOfWeek($date) {
$getdate = getdate($date);
// How many days ahead monday are we?
switch ( $getdate['wday'] ) {
case 0: // we are on sunday
$days = 6;
break;
default: // any other day
$days = $getdate['wday']-1;
break;
}
$seconds = $days*24*60*60;
$monday = date($getdate[0])-$seconds;
return $monday;
}
Thanks a ton
Not very smart, but would works for you:
// sept. 2012
$month = 9;
// loop through month days
for ($i = 1; $i <= 31; $i++) {
// given month timestamp
$timestamp = mktime(0, 0, 0, $month, $i, 2012);
// to be sure we have not gone to the next month
if (date("n", $timestamp) == $month) {
// current day in the loop
$day = date("N", $timestamp);
// if this is between 1 to 5, weekdays, 1 = Monday, 5 = Friday
if ($day == 1 OR $day <= 5) {
// write it down now
$days[$day][] = date("j", $timestamp);
}
}
}
// to see if it works :)
print_r($days);
I would like to get the days of the week compared to the present day. Example :
Today it's sunday 25, i would like to have an array with :
19, 20, 21, 22, 23, 24, 25 (monday, tuesday, etc)
If we were in the middle of the week, for example Wednesday 21, i should have the same array because it's the same week.
Thanks
Take a look at strtotime().
It accepts relative time strings, for example:
strtotime('last Monday');
From there you could determine the week for a loop, start and end dates, etc.
Note: Be mindful of your usage though. As noted in the comments this logic is not internationalized. Furthermore, strtotime() is a locale specific function.
This might do the trick:
$days = array();
$inputdate = time();
$dayOfInput = date('j', $inputdate); // 25
$weekdayOfInput = date('N', $inputdate); // monday = 1, tuesday = 2, ...
$monday = $dayOfInput - ($weekdayOfInput - 1);
for ($i = 0; $i < 7; $i++)
{
$days[$i] = $monday + $i;
}
var_dump($days);
I am trying to get an array of a date plus the next 13 dates to get a 14 day schedule starting from a given date.
here is my function:
$time = strtotime($s_row['schedule_start_date']); // 20091030
$day = 60*60*24;
for($i = 0; $i<14; $i++)
{
$the_time = $time+($day*$i);
$date = date('Y-m-d',$the_time);
array_push($dates,$date);
}
But it seems to be repeating a date when the month switches over..
this is what I get:
2009-10-30|2009-10-31|2009-11-01|2009-11-01|2009-11-02|2009-11-03|2009-11-04|2009-11-05|2009-11-06|2009-11-07|2009-11-08|2009-11-09|2009-11-10|2009-11-11
Notice that 2009-11-01 is repeated. I cannot figure out why?
What am I doing wrong?
Thanks!!
I would use strtotime
$start = strtotime($s_row['schedule_start_date']);
$dates=array();
for($i = 1; $i<=14; $i++)
{
array_push($dates,date('Y-m-d', strtotime("+$i day", $start)));
}
print_r($dates);
You have the same date because of daylight saving time switch. It's not safe to add 24*60*60 seconds to find next day, because 2 days in the year have more/less seconds in them. When you switch from summer to winter time you are adding 1 hour to a day. So it'll be 25*60*60 seconds in that day, that's why it's not switched in your code.
You can do your calculation by mktime(). For example:
## calculate seconds from epoch start for tomorrow
$tomorrow_epoch = mktime(0, 0, 0, date("m"), date("d")+1, date("Y"));
## format result in the way you need
$tomorrow_date = date("M-d-Y", $tomorrow_epoch);
Or the full version for your code:
$dates = array();
$now_year = date("Y");
$now_month = date("m");
$now_day = date("d");
for($i = 0; $i < 14; $i++) {
$next_day_epoch = mktime(0, 0, 0, $now_month, $now_day + $i, $now_year);
array_push(
$dates,
date("Y-m-d", $next_day_epoch)
);
}
I recommend something like:
for($i=1;$i<=14;$i++){
echo("$i day(s) away: ".date("m/d/Y",strtotime("+$i days")));
}