Subtract 1 day with PHP - php

I'm trying to take a date object that's coming out of my Drupal CMS, subtract one day and print out both dates. Here's what I have
$date_raw = $messagenode->field_message_date[0]['value'];
print($date_raw);
//this gives me the following string: 2011-04-24T00:00:00
$date_object = date_create($date_raw);
$next_date_object = date_modify($date_object,'-1 day');
print('First Date ' . date_format($date_object,'Y-m-d'));
//this gives me the correctly formatted string '2011-04-24'
print('Next Date ' . date_format($next_date_object,'Y-m-d'));
//this gives me nothing. The output here is always blank
So I'm not understanding why the original date object is coming out fine, but then I'm trying to create an additional date object and modify it by subtracting one day and it seems like I can't do that. The output always comes out blank.

You can try:
print('Next Date ' . date('Y-m-d', strtotime('-1 day', strtotime($date_raw))));

date('Y-m-d',(strtotime ( '-1 day' , strtotime ( $date) ) ));

$date = new DateTime("2017-05-18"); // For today/now, don't pass an arg.
$date->modify("-1 day");
echo $date->format("Y-m-d H:i:s");
Using DateTime has significantly reduced the amount of headaches endured whilst manipulating dates.

Object oriented version
$dateObject = new DateTime( $date_raw );
print('Next Date ' . $dateObject->sub( new DateInterval('P1D') )->format('Y-m-d');

A one-liner option is:
echo date_create('2011-04-24')->modify('-1 days')->format('Y-m-d');
Running it on Online PHP Editor.
mktime alternative
If you prefer to avoid using string methods, or going into calculations, or even creating additional variables, mktime supports subtraction and negative values in the following way:
// Today's date
echo date('Y-m-d'); // 2016-03-22
// Yesterday's date
echo date('Y-m-d', mktime(0, 0, 0, date("m"), date("d")-1, date("Y"))); // 2016-03-21
// 42 days ago
echo date('Y-m-d', mktime(0, 0, 0, date("m"), date("d")-42, date("Y"))); // 2016-02-09
//Using a previous date object
$date_object = new DateTime('2011-04-24');
echo date('Y-m-d',
mktime(0, 0, 0,
$date_object->format("m"),
$date_object->format("d")-1,
$date_object->format("Y")
)
); // 2011-04-23
Online PHP Editor

Not sure why your current code isn't working but if you don't specifically need a date object this will work:
$first_date = strtotime($date_raw);
$second_date = strtotime('-1 day', $first_date);
print 'First Date ' . date('Y-m-d', $first_date);
print 'Next Date ' . date('Y-m-d', $second_date);

Answear taken from Php manual strtotime function comments :
echo date( "Y-m-d", strtotime( "2009-01-31 -1 day"));
Or
$date = "2009-01-31";
echo date( "Y-m-d", strtotime( $date . "-1 day"));

You can add strtotime() in date() with parameter number of day week or month.
Example for day:
date("Y-m-d", strtotime("-1 day"));
For week:
date("Y-m-d", strtotime("-1 week"));
For month:
date("Y-m-d", strtotime("-1 months"));

How about this: convert it to a unix timestamp first, subtract 60*60*24 (exactly one day in seconds), and then grab the date from that.
$newDate = strtotime($date_raw) - 60*60*24;
echo date('Y-m-d',$newDate);
Note: as apokryfos has pointed out, this would technically be thrown off by daylight savings time changes where there would be a day with either 25 or 23 hours

Related

Find next occurrence of date

I want to find the next occurrence of date from the current date.
For example, imagine I want to find 20th of the month from current date
If current date is 10th october then return the result 2014-10-20 (Y-m-d)
If current date is 22nd october then return the result 2014-11-20 (Y-m-d)
I created a solution just now using a while loop.
$oldd= "2014-06-20";
$newdate = date("Y-m-d",strtotime($oldd."+1months"));
while(strtotime($newdate) <= strtotime(date("Y-m-d")))
{
$newdate = date("Y-m-d",strtotime($newdate."+1months"));
}
echo $newdate;
manually and pass that to strtotime(). The time information you need to extract from the reference time string. Like this:
$refdate = '2014-02-25 10:30:00';
$timestamp = strtotime($refdate);
echo date('Y-m-d H:i:s',
strtotime("next Thursday " . date('H:i:s', $timestamp), $timestamp)
);
same results could be achieved using string concatenation:
echo date('Y-m-d', strtotime("next Thursday", $timestamp)
. ' ' . date('H:i:s', $timestamp);
another way, you can use methods of DateTime object, PHP has really rich API in dealing with date time.
$current_date = new DateTime('2014-06-20');
if ($current_date->format('d') >= 20) {
// $current_date->modify('last day of this month')->modify("+20 days");
$current_date->modify('first day of next month')->modify("+19 days");
}else{
$current_date->modify('first day of this month')->modify("+19 days");
}
echo $current_date->format("Y-m-d");
http://php.net/manual/en/datetime.modify.php
http://php.net/manual/en/datetime.formats.relative.php

I need clarification in php date function

I am generating next date using the following code:
$s1=date('d/M/Y', strtotime('+1 day'));
echo $s1;
for ex: Assume current date is 26/Aug/2014.
so above code generates 27/Aug /2014 and storing in varible $s1.
By using the varible s1 i want to create 28/Aug/2014. how to create?
I dont want to use '+2 day' in STRTOTIME function. I want to generate next day based on variable $s1.
You can do it all with strtotime() but you have to remember that strtotime assumes a USA date format when it see's a / forward slash as a seperator.
So before using $s1 you need to convert the / to a - so it assumes a sensible data format is being used.
$s1=date('d/M/Y', strtotime('+1 day'));
echo $s1.PHP_EOL;
// change date format as strtotime assumes USA dates
$date = strtotime( '+1 day', strtotime( str_replace('/','-',$s1) ) );
echo date('d/M/Y', $date);
When run on 26/Aug/2014 the result would be
27/Aug/2014
28/Aug/2014
The best way (using strtotime):
$tomorrow = strtotime('+1 day');
$twoDaysHence = strtotime('+1 day', $tomorrow);
echo date('d/M/Y', $tomorrow);
echo date('d/M/Y', $twoDaysHence);
In other words, leave your date variables in the form of UNIX timestamps as returned by strtotime until you need to display them. Because you can do calculations directly with them in this format. Once you format that to a date string, you'll have to convert them back into a malleable form first. strtotime doesn't recognise the format d/M/Y automatically, so that makes that all the harder. You should use DateTime in that case:
$tomorrow = date('d/M/Y', strtotime('+1 day'));
$timestamp = DateTime::createFromFormat('d/M/Y', $tomorrow);
$timestamp->modify('+1 day');
echo $timestamp->format('d/M/Y');
You can use something like following:
$newvariable = strtotime ('+2 day' , $s1);
this is a very simple part
$s1=date('d/M/Y', strtotime('+2 day'));
echo $s1;
and if you want then copy the value of $s1 in another variable
function date_addDate($text, $da=0, $ma=0, $ya=0, $ha=0)
{
$h=date('H',strtotime($text));
$d=date('d',strtotime($text));
$m=date('m',strtotime($text));
$y=date('Y',strtotime($text));
$fromTime =date("Y-m-d H:i:s", mktime($h+$ha, 0, 0, $m+$ma, $d+$da, $y+$ya));
return $fromTime;
}
$date = date("Y-m-d H:i:s");
// $da days
// $ma months
// $ya years
// $ha hours
echo date_addDate($date, $da=0, $ma=0, $ya=0, $ha=0);
//out put : current date
echo date_addDate($date, $da=2, $ma=0, $ya=0, $ha=0);
//out put : As you want
Try this
$s1=date('d/M/Y', strtotime('+1 day'));
echo $s1; echo "<br/>";
$date = strtotime(strtotime($s1). ' + 2 days');
$s2 = date('d/M/Y', $date);
echo $s2;
Now it's edited!! Check it!
What about using DateTime ?
$d1 = new DateTime(date('Y-m-d'));
$d1->format('d/m/Y'); // 26/08/2014
$d1->modify('+1 day');
$d1->format('d/m/Y'); // 27/08/2014

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"));

How can I get a date after 15 days/1 month in PHP?

In my PHP code I have a date in my variable "$postedDate".
Now I want to get the date after 7 days, 15 days, one month and 2 months have elapsed.
Which date function should I use?
Output date format should be in US format.
Use strtotime.
$newDate = strtotime('+15 days',$date)
$newDate will now be 15 days after $date. $date is unix time.
http://uk.php.net/strtotime
try this
$date = date("Y-m-d");// current date
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +2 week");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 month");
$date = strtotime(date("Y-m-d", strtotime($date)) . " +30 days");
Since PHP 5.2.0 the DateTime build in class is available
$date = new DateTime($postedDate);
$date->modify('+1 day');
echo $date->format('Y-m-d');
http://php.net/manual/en/class.datetime.php
$date=strtotime(date('Y-m-d')); // if today :2013-05-23
$newDate = date('Y-m-d',strtotime('+15 days',$date));
echo $newDate; //after15 days :2013-06-07
$newDate = date('Y-m-d',strtotime('+1 month',$date));
echo $newDate; // after 1 month :2013-06-23
This is very simple; try this:
$date = "2013-06-12"; // date you want to upgade
echo $date = date("Y-m-d", strtotime($date ." +1 day") );
What’s the input format anyway?
1) If your date is, say, array of year, month and day, then you can mktime (0, 0, 0, $month, $day + 15, $year) or mktime (0, 0, 0, $month + 1, $day, $year). Note that mktime is a smart function, that will handle out-of-bounds values properly, so mktime (0, 0, 0, 13, 33, 2008) (which is month 13, day 33 of 2008) will return timestamp for February, 2, 2009.
2) If your date is a timestamp, then you just add, like, 15*SECONDS_IN_A_DAY, and then output that with date (/* any format */, $postedDate). If you need to add one month 30 days won’t of course always work right, so you can first convert timestamp to month, day and year (with date () function) and then use (1).
3) If your date is a string, you first parse it, for example, with strtotime (), then do whatevee you like.

Categories