I need clarification in php date function - php

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

Related

get wrong value after convert date to day

I'm trying to convert date (29/04/2017) to day in week but I get wrong value (Thursday, but need to be Saturday). I check the time of the server, all correct.
my code:
$timestamp = strtotime("29/04/2017");
$day = date('l', $timestamp);
echo $day;
what can be the problem?
When you use slashes in your date, strtotime() assumes MM/DD/YYYY. You either need to change the format of your date or use DateTime::createFromFormat() to parse the date
$timestamp = strtotime("04/29/2017");
$day = date('l', $timestamp);
echo $day;
or
$date = DateTime::createFromFormat('d/m/Y, '29/04/2017);
$day = $date->format('l');
echo $day;

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

Add days to a timestamp

Im trying to add a certain amount of days to a timestmp using this in PHP:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo $endDate2;
but its displaying: 1216526400
any ideas?
Try:
echo date("Y-m-d H:i:s",$endDate2);
Or (for just the date):
echo date("Y-m-d",$endDate2);
You can find documentation about how to format your string here: http://php.net/manual/en/function.date.php
You should be using DateTime for working with dates. It's timezone friendly.
$datetime = new DateTime('2008-06-20');
$datetime->modify('+1 day');
echo $datetime->getTimestamp();
strtotime() converts the date into a unix timestamp which is the number of seconds since January 1st 1970. If you want a date output you have to run the finished timestamp through date() first.
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate.' +1 day');
echo date("Y-m-d", $endDate);
strtotime creates a Unix timestamp so if you want to be presented with a formatted date, you need to pass the timestamp as an argument to the date function as follows:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo date('Y-m-d', $endDate2);
Additionally, there are a wide variety of parameters you can use in the date function if you want to display additional information.
e.g.: echo date('Y-m-d H:i:s', $endDate2); or echo date('Y-m-d h:i:s a', $endDate2);, etc.
Sooooo close, just take your timestamp and convert it back into date format using date("desired format",$endDate2);
DateTime is a very nice way to deal with dates. You can try like this:
$capturedDate = '2008-06-20';
$date = DateTime::createFromFormat('Y-m-d', $capturedDate)->modify('+1 day');
echo $date->getTimestamp();

Subtract 1 day with 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

Categories