Php: date +1 displays 1970 - php

I'm getting the output of:
Warning: strtotime() expects parameter 2 to be long, string given in C:\xampp\htdocs\MindWeather\Assimilation\foreca_hourly.php on line 17
Today is 2014117, Tomorrow is 1970101
My code is:
$TodayIs = date('Ynd');
$nextdate = date('Ynd', strtotime($TodayIs,'+ 1 day'));
echo "<br><br> Today is $TodayIs, Tomorrow is $nextdate<br><br>";
I really don't expect to get a 1970-answer. It's supposed to display "Today is 2014117, Tomorrow is 2014118" instead of "Today is 2014117, Tomorrow is 1970101"

The second parameter to strtotime defaults to current time. So the following should produce desired result:
$nextdate = date('Ynd', strtotime('+1 day'));
// Today is 2014117, Tomorrow is 2014118

Why don't you make use of a DateTime Class ?
<?php
$date = new DateTime();
echo "Today is ".$date->format('Y/m/d');
$date->add(new DateInterval('P1D'));
echo "<br>Tomorrow is ".$date->format('Y/m/d');
OUTPUT :
Today is 2014/01/17Tomorrow is 2014/01/18

Try like this:
$TodayIs = date('Ynd');
$nextdate = date('Ynd', strtotime('+ 1 day'));
echo "<br><br> Today is $TodayIs, Tomorrow is $nextdate<br><br>";
PHPfiddle link: http://phpfiddle.org/lite/code/bnf-scw

Try this:
$date = '2013-10-15';
$new_time = strtotime('+1 day', strtotime($date));
echo date('d-m-Y', $new_time); //displays 2013-10-16

Related

How to get the current date in PHP, and add 1 month to the current date?

I'm coding a script where I require to save the current date, and the date 1 month from that date. I am pretty sure that the time() variable works, but I am not sure how to +1 month onto that?
Any ideas, suggestions. Cheers!
Try this
$today = date("Y-m-d");
$date = date('Y-m-d', strtotime('+1 month', $today));
or use DateTime()
$dt1 = new DateTime();
$today = $dt1->format("Y-m-d");
$dt2 = new DateTime("+1 month");
$date = $dt2->format("Y-m-d");
$time = strtotime("2010-12-11");
$final = date("Y-m-d", strtotime("+1 month", $time));
(OR)
strtotime( "+1 month", strtotime( $time ) );
this returns a timestamp that can be used with the date function
Use this:
Current Date:
echo "Today is " . date("Y/m/d");
1 Month to the Current Date:
$time = strtotime(date("Y/m/d"));
$final = date("Y-m-d", strtotime("+1 month", $time));
<?php
$current_time = date("Y-M-d h:i:s",time()); // Getting Current Date & Time
print $current_time; // Current Date & Time Printing for display purpose
$future_timestamp = strtotime("+1 month"); // Getting timestamp of 1 month from now
$final_future = date("Y-M-d h:i:s",+$future_timestamp); // Getting Future Date & Time of 1 month from now
print $final_future; // Printing Future time for display purpose
?>
shorter : $today=date("Y-m-d"); $date=
This one liner worked for me:
$monthFromToday = date("Y-m-d", strtotime("+1 month", strtotime(date("Y/m/d"))));
The given answers may not give you the results you might expect or desire.
Consider:
$today = "29Jan2018";
$nextMonth = date('dMY', strtotime('+1 month', (strtotime($today))));
echo $today // yields 29Jan2018
echo $nextMonth // yields 01Mar2018
$today = date("Y-m-d");
$enddate = date('Y-m-01',strtotime($today. ' + 1 months'));
You could also consider using the Carbon package.
The solution would look like this:
use Carbon\Carbon
$now = Carbon::now;
$now->addMonth();
Here is the link for reference https://carbon.nesbot.com/docs/

PHP: Incorrect strtotime()

I had gone through various stackoverflow solutions and other blogs but still it doesn't fix my problem.
Let's say that the date today is: 2013-12-28 and I want to get the date after 1 month and it is supposed to display 2014-01-28.
$date = date('o-m-d');
$final = date('o-m-d', strtotime("+1 month", $date));
echo $final;
Above is my code. It returns 02/01/1970.
I have also tried the mktime method but still it displays the 1970 output.
What am I doing wrong?
BTW. I am working this on a hosted server.
Thanks ahead. :)
Use DateTime function modify
$date = new DateTime( 'o-m-d' );
echo $date->modify( '+1 month' )->format('o-m-d');
If you want the current date +1 month use:
$final = date('o-m-d', strtotime("+1 month"));
Or with a given date:
$date = date('o-m-d');
$final = date('o-m-d', strtotime($date . " +1 month"));
echo $final;
If you want to use the second parameter of strtotime it has to be a timestamp.
Go the OOP way..
<?php
$date = new DateTime('2013-12-28');
$date->add(new DateInterval('P1M'));
echo $date->format('Y-m-d'); //prints 2014-01-28

How to get Dates of Previous or Next Months in PHP?

I know that date("Y"), date("m") and date("d") will return current Year (2013), Month (07) and Date (11) respectively.
I am working with date Format: "2013-07-11". I have current date like this. Now I want to get the value "2013-06-11" and "2013-08-11" somehow using PHP.
What might be the code to get this values (Last Month's Same Date, and Next Month's Same Date)?
I tried:
$LastMonth = date ("m") - 1;
$LastDate = date("Y") . "-0" . $LastMonth . "-" . date("d");
But this will return error when it is October. In October it will show "2013-010-11".
What can be a better solution? Can anyone help me?
Use it with PHP's strtotime():
echo date('Y-m-d', strtotime('+1 month')); //outputs 2013-08-11
echo date('Y-m-d', strtotime('-1 month')); //outputs 2013-06-11
$date = new DateTime( "2013-07-11");
$date->modify("+1 month");
echo $date->format(‘l, F jS, Y’);
Try this
$lst_month=mktime(0,0,0,date('m')-1,date('d'),date('Y'));
echo "M<br>". date("Y-m-d",$lst_month);
$next_month=mktime(0,0,0,date('m')+1,date('d'),date('Y'));
echo "M<br>". date("Y-m-d",$next_month);
$nextmonth=date("dmy",strtotime("+1 month"));
$lastmonth=date("dmy",strtotime("-1 month"));

php - find date for same day of week for last year

So, in PHP i'm trying to return a date value for last year based on the same day of week.
EX: (Monday) 2011-12-19 inputted should return (Monday) 2010-12-20.
I was just doing it simply by -364 but then that was failing on leap years. I came across another function :
$newDate = $_POST['date'];
$newDate = strtotime($newDate);
$oldDate = strtotime('-1 year',$newDate);
$newDayOfWeek = date('w',$oldDate);
$oldDayOfWeek = date('w',$newDate);
$dayDiff = $oldDayOfWeek-$newDayOfWeek;
$oldDate = strtotime("$dayDiff days",$oldDate);
echo 'LAST YEAR DAY OF WEEK DATE = ' . date('Ymd', $oldDate);
however, that is failing when you try to input a Sunday date, as it does a 0 (sunday) minus 6 (saturday of last year date), and returns with a value T-6. IE inputting 2011-12-25 gets you 2010-12-19 instead of 2011-12-26.
I'm kind of stumped to find a good solution in php that will work for leap years and obviously all days of the week.
Any suggestions?
Thanks!
How about this, using PHP's DateTime functionality:
$date = new DateTime('2011-12-25'); // make a new DateTime instance with the starting date
$day = $date->format('l'); // get the name of the day we want
$date->sub(new DateInterval('P1Y')); // go back a year
$date->modify('next ' . $day); // from this point, go to the next $day
echo $date->format('Ymd'), "\n"; // ouput the date
$newDate = '2011-12-19';
date_default_timezone_set('UTC');
$newDate = strtotime($newDate);
$oldDate = strtotime('last year', $newDate);
$oldDate = strtotime(date('l', $newDate), $oldDate);
$dateFormat = 'Y-m-d l w W';
echo "This date: ", date($dateFormat, $newDate), "\n";
echo "Old date : ", date($dateFormat, $oldDate);
That gives:
This date: 2011-12-19 Monday 1 51
Old date : 2010-12-20 Monday 1 51
Use strtotime() to get a date, for the same week last year.
Use the format {$year}-W{$week}-{$weekday}, like this:
echo date("Y-m-d", strtotime("2010-W12-1"));
And you can do that for as long back you wan't:
<?php
for($i = 2011; $i > 2000; $i--)
echo date("Y-m-d", strtotime($i."-W12-1"));
?>
Make it easier :)
echo date('Y-m-d (l, W)').<br/>;
echo date('Y-m-d (l, W)', strtotime("-52 week"));
Edit: I forgot to write output: :)
2015-05-06 (Wednesday, 19)
2014-05-07 (Wednesday, 19)
<?php
$date = "2020-01-11";
$newdate = date("Y-m-d",strtotime ( '-1 year' , strtotime ( $date ) )) ;
echo $newdate;
?>
ref https://www.nicesnippets.com/blog/how-to-get-previous-year-from-date-in-php

Getting last month's date in php

I want to get last month's date. I wrote this out:
$prevmonth = date('M Y');
Which gives me the current month/year. I can't tell if I should use strtotime, mktime. Something to the timestamp? Do I need to add something afterwards to reset so that the date isn't set to last month throughout everything for all timestamps on my site? I'm trying to RTM but it's hard for me to figure this out.
It's simple to get last month date
echo date("Y-n-j", strtotime("first day of previous month"));
echo date("Y-n-j", strtotime("last day of previous month"));
at November 3 returns
2014-10-1
2014-10-31
echo strtotime("-1 month");
That will output the timestamp for last month exactly. You don't need to reset anything afterwards. If you want it in an English format after that, you can use date() to format the timestamp, ie:
echo date("Y-m-d H:i:s",strtotime("-1 month"));
$prevmonth = date('M Y', strtotime("last month"));
Incorrect answers are:
$lastMonth = date('M Y', strtotime("-1 month"));
$lastDate = date('Y-m', strtotime('last month'));
The reason is if current month is 30+ days but previous month is 29 and less $lastMonth will be the same as current month.
e.g.
If $currentMonth = '30/03/2016';
echo $lastMonth = date('m-Y', strtotime("-1 month")); => 03-2016
echo $lastDate = date('Y-m', strtotime('last month')); => 2016-03
The correct answer will be:
echo date("m-Y", strtotime("first day of previous month")); => 02-2016
echo sprintf("%02d",date("m")-1) . date("-Y"); => 02-2016
echo date("m-Y",mktime(0,0,0,date("m")-1,1,date("Y"))); => 02-2016
echo date('Y',strtotime("-1 year")); //last year<br>
echo date('d',strtotime("-1 day")); //last day<br>
echo date('m',strtotime("-1 month")); //last month<br>
Found this one wrong when the previous months is shorter than current.
echo date("Y-m-d H:i:s",strtotime("-1 month"));
Try on March the 30th and you will get 2012-03-01 instead of 2012-02...
Working out on better solution...
if you want to get just previous month, then you can use as like following
$prevmonth = date('M Y', strtotime('-1 months'));
if you want to get same days of previous month, Then you can use as like following ..
$prevmonth = date('M Y d', strtotime('-1 months'));
if you want to get last date of previous month , Then you can use as like following ...
$prevmonth = date('M Y t', strtotime('-1 months'));
if you want to get first date of previous month , Then you can use as like following ...
$prevmonth = date('M Y 1', strtotime('-1 months'));
public function getLastMonth() {
$now = new DateTime();
$lastMonth = $now->sub(new DateInterval('P1M'));
return $lastMonth->format('Ym');
}
Use this short code to get previous month for any given date:
$tgl = '25 january 2012';
$prevmonth = date("M Y",mktime(0,0,0,date("m", strtotime($tgl))-1,1,date("Y", strtotime($tgl))));
echo $prevmonth;
The result is December 2011.
Works on a month with shorter day with previous month.
$lastMonth = date('M Y', strtotime("-1 month"));
var_dump($lastMonth);
$lastMonth = date('M Y', mktime(0, 0, 0, date('m') - 1, 1, date('Y')));
var_dump($lastMonth);
Simply get last month.
Example:
Today is: 2020-09-02
Code:
echo date('Y-m-d', strtotime(date('Y-m-d')." -1 month"));
Result:
2020-08-02
You can use strtotime, which is great in this kind of situations :
$timestamp = strtotime('-1 month');
var_dump(date('Y-m', $timestamp));
Will get you :
string '2009-11' (length=7)
$time = mktime(0, 0, 0, date("m"),date("d")-date("t"), date("Y"));
$lastMonth = date("d-m-Y", $time);
OR
$lastMonth = date("m-Y", mktime() - 31*3600*24);
works on 30.03.2012
Oh I figured this out, please ignore unless you have the same problem i did in which case:
$prevmonth = date("M Y",mktime(0,0,0,date("m")-1,1,date("Y")));
This question is quite old but here goes anyway. If you're trying to get just the previous month and the day does not matter you can use this:
$date = '2014-01-03';
$dateTime = new DateTime($date);
$lastMonth = $dateTime->modify('-' . $dateTime->format('d') . ' days')->format('F Y');
echo $lastMonth; // 'December 2013'
The best solution I have found is this:
function subtracMonth($currentMonth, $monthsToSubtract){
$finalMonth = $currentMonth;
for($i=0;$i<$monthsToSubtract;$i++) {
$finalMonth--;
if ($finalMonth=='0'){
$finalMonth = '12';
}
}
return $finalMonth;
}
So if we are in 3(March) and we want to subtract 5 months that would be
subtractMonth(3,5);
which would give 10(October). If the year is also desired, one could do this:
function subtracMonth($currentMonth, $monthsToSubtract){
$finalMonth = $currentMonth;
$totalYearsToSubtract = 0;
for($i=0;$i<$monthsToSubtract;$i++) {
$finalMonth--;
if ($finalMonth=='0'){
$finalMonth = '12';
$totalYearsToSubtract++;
}
}
//Get $currentYear
//Calculate $finalYear = $currentYear - $totalYearsToSubtract
//Put resulting $finalMonth and $finalYear into an object as attributes
//Return the object
}
It works for me:
Today is: 31/03/2012
echo date("Y-m-d", strtotime(date('m', mktime() - 31*3600*24).'/01/'.date('Y').' 00:00:00')); // 2012-02-01
echo date("Y-m-d", mktime() - 31*3600*24); // 2012-02-29
If you want to get first date of previous month , Then you can use as like following ... $prevmonth = date('M Y 1', strtotime('-1 months')); what? first date will always be 1 :D

Categories