I have the following code in order to create a previous and next month links:
$date = mktime( 0, 0, 0, date("m"), 1, date("y") );
$nextMonth = strftime( '%B', strtotime( '+1 month', $date ) );
$prevMonth = strftime( '%B', strtotime( '-1 month', $date ) );
$nextYear = strftime( '%Y', strtotime( '+1 month', $date ) );
$prevYear = strftime( '%Y', strtotime( '-1 month', $date ) );
Now when i get to december
the next year variable 2016
the previous year variable 2015
But when the date is now january 2016
the next year variable 2015
the previous year variable 2014
I can't understand why its doing that or what i am doing wrong here. Any one have any idea how to fix this?
Testing your function with fixed parameters, you'll get :
$date = mktime( 0, 0, 0, 1, 1, 2016);
and echo $nextYear will not give you 2016 because you only added 1 month. Testing it does what's scripted :
the next year variable 2016 // which is january 2016 + 1 month = february 2016
the previous year variable 2015 // jan 2016 - 1 month = dec 2015
My guess is you are passing date('Y') without realizing it's 2015
Related
I'm trying to get the current, the next and the after next month by php.
This is my current php:
setlocale(LC_TIME, "de_DE.utf8");
$thismonth = strftime("%B");
$nextmonth = strftime( '%B', strtotime( '+1 month', mktime( 0, 0, 0, $month, 1, $year ) ) );
$overnextmonth = strftime( '%B', strtotime( '+2 month', mktime( 0, 0, 0, $month, 1, $year ) ) );
The month name should be in german so I'm using setlocale(LC_TIME, "de_DE.utf8");.
The ouput is: Januar (january), Januar (january), Februar (february). I can't figure the issue out.
use this setlocale (LC_TIME,"de_DE"); it will work
<?php
setlocale (LC_TIME,"de_DE");
echo $thismonth = strftime("%B");
echo $nextmonth = strftime( '%B', strtotime( '+1 month', mktime( 0, 0, 0, 1, 1, 2017 ) ) );
echo $overnextmonth = strftime( '%B', strtotime( '+2 month', mktime( 0, 0, 0, 2, 1, 2017 ) ) );
?>
output
output picture
I suggest to move away from strtotime and use the php dateTime class.
$date = new DateTime();
$date->add(new DateInterval('P1M')); // <-- adds 1 month to the current
echo $date->format('F') . "\n"; // it's now January so it outputs February
$date->add(new DateInterval('P1M'));
echo $date->format('F') . "\n"; // adds one more (total 2) months from original
Current month is January. This output will be "February March"
Every Sunday, the strtotime function is doing weird stuff on my schedule website. It shows next week's date instead of +2 weeks.
My code:
date_default_timezone_set("Europe/Amsterdam");
$time = time();
//Check the date of Monday and Sunday from 2 weeks later.
$monday = date( 'd-m-Y', strtotime( '+1 week monday' ) );
$sunday = date( 'd-m-Y', strtotime( '+2 week sunday' ) );
//Check the weeknumber of this week and add 2, so that it shows the weeknumber of 2 weeks later.
$weekdata = date("W", strtotime('+1 day', $time));
$weeknumber = $weekdata + 2;
$Weeknumber shows correct information: 42, but $monday and $sunday shows next week's monday and sunday (03-10-2016 | 09-10-2016), but I want to have it show (10-10-2016 | 16-10-2016)
Also, is there a way to make the weeks start at monday instead of sunday? Because, on the "this week" page, it already shows data from week 26-09-2016 | 02-10-2016 instead of 19-09-2016 | 25-10-2016.
The strtotime function bug happens on PHP version 5.5.31. Upgrade your PHP version or try the fix below.
So I've fixed the bug with an if/else statement.
$time = time();
//Check the date of monday and sunday from this week.
//Strtotime() bug on PHP version 5.5.31. Bug report: https://bugs.php.net/bug.php?id=63740
if(date("l") == "Sunday"){
$monday = date( 'd-m-Y', strtotime( 'monday last week' ) );
$sunday = date( 'd-m-Y', strtotime( 'sunday last week' ) );
//Check the weeknumber of this week.
$weeknumber = date("W", strtotime('+0 day', $time));
} else {
$monday = date( 'd-m-Y', strtotime( 'monday this week' ) );
$sunday = date( 'd-m-Y', strtotime( 'sunday this week' ) );
//Check the weeknumber of this week.
$weeknumber = date("W", strtotime('+1 day', $time));
}
Since the bug only happened on sunday, I made the if/else statement only check for sunday and not for the other days. If today is not sunday, it runs the else code, which is the normal way of getting this week's data. I'll see if the code works 100% when today is not sunday, but another day of the week.
I just wanted to show my solution to this problem to the people who's going to visit this question in the future.
I am trying to get the timestamp of the first day of the month at 00:00:00.
For example, the timestamp of Jan 01 2012 00:00:00
I'm doing this:
$test_month = time(); // Seconds
$test_month = date('M', $test_month); // This month
$test_month = strtotime($test_month); // Timestamp of this month
echo $test_month;
This is returning zero hour of the current day of the month, not the start of the month.
Any suggestions?
Try this:
echo date( 'F jS Y h:i:s A', strtotime( 'first day of ' . date( 'F Y')));
This will output:
June 1st 2012 12:00:00 AM
Try this
$time = strtotime(date('Y-m-01 00:00:00')); // == 1338534000
Which translates to:
$date = date('Y-m-d H:i:s', $time); // == 2012-06-01 00:00:00
Read the PHP manual on the date() and time() functions.
echo date("Y-m-d 00:00:00", strtotime("first day of this month"));
This outputs:
2017-05-01 00:00:00
how to get the previous 3 months in php ex(If i say DEC.. It should display the previous 3 months i.e., OCT NOV DEC)
You can use the strtotime function like this:
echo date('M', strtotime('-3 month'));
So you specify previous dates with minus sign.
echo date('M', strtotime('0 month'));
echo date('M', strtotime('-1 month'));
echo date('M', strtotime('-2 month'));
echo date('M', strtotime('-3 month'));
Results:
Dec
Nov
Oct
Sep
You can do the same if you are using a loop like this:
for ($i = -3; $i <= 0; $i++){
echo date('M', strtotime("$i month"));
}
Results:
Sep
Oct
Nov
Dec
Check out the documentation too see many other friendly date and time keywords strtotime supports:
http://php.net/manual/en/function.strtotime.php
Voted answer is almost correct.
Correct solution is:
for ($i = -3; $i <= 0; $i++){
echo date('M', strtotime("$i month", strtotime(date("Y-m-15"))));
}
Explain: strtotime default implementation is:
date ( string $format [, int $timestamp = time() ] ) : string
As you can see there is timestamp with default value of: time().
Last say today is 31th March.
Because there is no 31th Feb
echo date('M', strtotime("-1 month"));
will return March
On the other hand:
echo date('M', strtotime("+1 month"));
will return May (April will be skipped).
Because of that issue we have to setup timestamp value which is a safe date.
Every date between 1-28 is safe because every month have got that date. In my example I had choose 15th day of month.
$month = date('M');
$year = date('Y');
$no_of_mnths = date('n',strtotime("-2 months"));
$remaining_months = (12 - $no_of_mnths)."\r\n";
$tot = $remaining_months+1;
for($j=0;$j<$tot;$j++){
echo date('M',strtotime(''.$no_of_mnths.' months'))
$no_of_mnths++;
}
This may help you`
<?php
$date = explode("-", "2016-08-31");
if($date[2]== "01"){$days = "-0 days";}else{$days = "-1 days";}
$time = strtotime($days, mktime(0, 0, 0, $date[1], $date[2], $date[0]));
$MTD = date('Y-m-01', strtotime('0 month'));
$M1 = date('Y-m-01', strtotime('-1 month'));
$M2 = date('Y-m-01', strtotime('-2 month'));
$M3 = date('Y-m-01', strtotime('-3 month'));
$rng = array();
$rng[$MTD] = strftime("%B, %Y", strtotime(" ", $time));
$rng[$M1] = strftime("%B, %Y", strtotime("first day of previous month", $time));
$rng[$M2] = strftime("%B, %Y", strtotime("-2 months", $time));
$rng[$M3] = strftime("%B, %Y", strtotime("-3 months", $time));
?>
Here $MTD ,$M1,$M2,$M3 will give date in the form of "dd-mm-yyyy" and $rng[$MTD], $rng[$M1],$rng[$M2],$rng[$M3] give date in the form of"Name of Month,year" => (i.e August,2016)
In my application a week is defined from Monday 12:00:00 AM to Sunday 11:59:59 PM
Whenever a user visits my site - I need to find the previous weeks date range and show him results based on that. It sounds simple but I'm lost.
To give you scenarios -
- March 1st Monday 12:00:00 AM to March 7th Sunday 12:59:59 PM is the week.
Now when a user visits the website on 8th March or 10th March or 12th March - based on the current date I should be able to get the previous week date range ie start date March 1st and end date March 7th.
But if the user visits the site say on 16th March - the date range I would need is March 8th to March 15th.
How can I do this in PHP.
Thanks
You could try doing it with timestamps, but that gets messy with timezone changes (for example, CET -> CEST). I'd use the DateTime class:
$d = new DateTime();
$weekday = $d->format('w');
$diff = 7 + ($weekday == 0 ? 6 : $weekday - 1); // Monday=0, Sunday=6
$d->modify("-$diff day");
echo $d->format('Y-m-d') . ' - ';
$d->modify('+6 day');
echo $d->format('Y-m-d');
The strtotime function is very handy here:
$mondayStr = "last monday";
if (date('N') !== '1') { // it's not Monday today
$mondayStr .= " last week";
}
$monday = strtotime($mondayStr);
echo date('r', $monday); // Mon, 22 Feb 2010 00:00:00 +1000
$sunday = strtotime('next monday', $monday) - 1;
echo date('r', $sunday); // Sun, 28 Feb 2010 23:59:59 +1000
function get_week_start($year, $month, $day)
{
$timestamp = mktime(0, 0, 0, $month, $day, $year);
return date('F j Y', $timestamp = mktime(0, 0, 0, $month, date('d', $timestamp)-date('w', $timestamp), $year));
}
You could perhaps add the next 6 days and you have it.
There is a user function for this in the PHP Documentation.
GMT version
$prev_monday_t = time() - (gmdate('N') + 6) * 86400;
$prev_sunday_t = time() - gmdate('N') * 86400;
echo gmdate('Y-m-d H:i:s', $prev_monday_t ).' '.gmdate('Y-m-d H:i:s', $prev_sunday_t );
Local version
$prev_monday_t = time() - (date('N') + 6) * 86400;
$prev_sunday_t = time() - date('N') * 86400;
echo date('Y-m-d H:i:s', $prev_monday_t ).' '.date('Y-m-d H:i:s', $prev_sunday_t );