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"
Related
I am trying to get third previous month from inputted month-year ie. if I give 06-2016 as an input, it should give me 03-2016 as result. I have tried strtotime("-3 Months"). But it gives me just 3 months from current date time.
Can someone tell me how to solve it please.
I have tried:
$month = 06;
$year = 2016;
$d = mktime(0, 0, 0, $month, 1, $year);
$td = date("Y-m-d", $d);
$oldDate = date('Y-m-d', strtotime("-3 Months"));
It gives me 3 months from current date. Any guesses how do I get the expected result ?
Use below. You need to use date("F Y", strtotime( INPUT_DATE ." -3 months"))
$month = 06;
$year = 2016;
$d = mktime(0, 0, 0, $month, 1, $year);
$td = date("Y-m-d", $d);
echo $olddate = date("Y-m-d", strtotime( $td ." -3 months"));
echo $months = date("F Y", strtotime( $td ." -3 months"));
Output:
2016-03-01
March 2016
Online Demo: Click Here
strtotime of (Input Date ."-3 Months")
$month = 06;
$year = 2016;
$d = mktime(0, 0, 0, $month, 1, $year);
$td = date("Y-m-d", $d);
$oldDate = date('Y-m-d', strtotime($td."-3 Months"));
$oldDate_MonthYear = date('m-Y', strtotime($td."-3 Months"));
echo $oldDate;
echo '<br />';
echo $oldDate_MonthYear;
Use Object Based.
$date1 = new DateTime("2016-06-01");
echo $startDate = $date1->modify("-3 month")->format('Y-m-d');
Try this and you should get desired result.
$prev_month_ts = strtotime('2016-06 -3 month');
$prev_month = date('Y-m', $prev_month_ts);
echo $prev_month;
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
sorry if this is a duplicate. Why does the following code error so? It returns Dec 6th currently, the first Friday in December (asking on 8 Oct 2013)
$thisMonth = date('m');
$year = date("Y");
$thismonthName = date("M.", mktime(0, 0, 0, $thisMonth,0,$year));
if ($thisMonth < 12) {
$nextmonthName = date("M.", mktime(0, 0, 0, $nextMonth,1,$year));
} else {
$nextMonth = 1;
$nextmonthName = date("M.", mktime(0, 0, 0, $nextMonth,1,$nextYear));
}
$thisDate = date('M j', strtotime($nextmonthName . $year . "first friday"));
print ("second friday next month is " . $thisDate);
but modifying it to be only next month, like so,
$thisMonth = date('m');
$year = date("Y");
$thismonthName = date("M.", mktime(0, 0, 0, $thisMonth,0,$year));
if ($thisMonth < 12) {
$nextmonthName = date("M.", mktime(0, 0, 0, $nextMonth,0,$year));
} else {
$nextMonth = 1;
$nextmonthName = date("M.", mktime(0, 0, 0, $nextMonth,0,$nextYear));
}
$thisDate = date('M j', strtotime($nextmonthName . $year . "first friday"));
print ("second friday next month is " . $thisDate);
returns Nov 8th, the second Friday in November. Why is that?
You REALLY should investigate DateTime, DateInterval, DatePeriod classes. They make this sort of thing trivial.
$date = new DateTime();
$interval = DateInterval::createFromDateString('second friday of next month');
$date->add($interval);
echo 'Second Friday next month is ' . $date->format('Y-m-d');
Or to get 2nd Friday for next 3 months:
$date = new DateTime();
$recurrence_count = 3;
$interval = DateInterval::createFromDateString('second friday of next month');
$period = new DatePeriod($date, $interval, $recurrence_count);
foreach ($period as $dt) {
echo $dt->format('Y-m-d');
}
Your code is way too complicated.
$secondNextFriday = new DateTime('first friday of +2 months'); // First friday in december
I almost have this by
$this->fecha = '2013 05-05 05:55:05'
$yearvalue = date("Y", strtotime($this->fecha) );
$monthname = date("F", strtotime($this->fecha) );
$dayvalue = date("d", strtotime($this->fecha) );
$printFecha = "$dayvalue of $monthname $yearvalue";
Which outputs 5 of May 2013
How can I get the name of the day of the week?
You can do it like this,
echo date('l j \of F Y', $this->date);
Your output will be something like this
Sunday 5 of May 2013
Using date of 2005-05-12:
echo date("l", mktime(0, 0, 0, 5, 12, 2005));
you'd get 'Thursday'.
Comment is the output I'm seeing. Help!
echo date("Y-m-t", strtotime("2012-07-31 -1 month")); //2012-07-31
echo date("Y-m-t", strtotime("2012-07-31 -2 month")); //2012-05-31
July has 31 days, so it can't be processing it as August 1, right? Even if it were, the second line should work. Right?!
Thanks!
What's happening here is, "7-31" minus one month comes out as "6-31", which translates to "7-1", which - asking for the last day of the month - comes out as "7-31".
When you want to do month math, it's almost always, if not always, better to build the time using mktime.
$month = 7;
echo date("Y-m-t", mktime(0, 0, 0, $month - 1, 1, 2012)); //2012-06-30
Note that when doing month math in mktime, always give '1' as the day. Or really anything as long as it's 28 or lower.
You can do something like this:
$month = date('m') ;
$day = date('d') ;
$year = date('Y') ;
// ----
$current = mktime( 0, 0, 0, $month , $day, $year);
$yesterday = mktime( 0, 0, 0, $month , $day-1, $year);
$tomorrow = mktime( 0, 0, 0, $month , $day+1, $year);
// ----
echo '<pre>Current Day: '.date('Y-m-d', $current).'</pre>';
echo '<pre>Day Before: '.date('Y-m-d', $yesterday).'</pre>';
echo '<pre>Day After: '.date('Y-m-d', $tomorrow).'</pre>';
If you're looking for the last day of July:
$july = mktime( 0, 0, 0, 8, 1-1, 2012);
// Last Day of July: 2012-07-31
echo '<pre>Last Day of July: '.date('Y-m-d', $july).'</pre>';
Using mktime, just enter the first day of August and subtract one day.