I a using PHP and would like the date of third day before the script is called in the format of YYYYMMDD. How can I do this?
Try this
$result = date('Y.m.d',strtotime("-3 days"));
Use DateTime:
$date = new \DateTime('-3 days');
echo $date->format('Ymd');
// Alternatively, store the string in a variable
$result = $date->format('Ymd');
try this easy way
echo date('Ymd', strtotime("-3 days"));
Atleast visit php.net once before such questions
<?php
$date = mktime(0, 0, 0, date("m") , date("d")-3, date("Y"));
echo date('Ymd', strtotime($date));
?>
Related
I have a date which is say like this
$given_date = '2014-12-25'; //Y-m-d format
Now i want to get the midnight timestamp of the given date, so I am doing this method
$midnight = strtotime(date('Y-m-d',$given_date).' 00:00:00');
Am I doing it right??
or I can use something like this?
$midnight = strtotime("midnight $given_date");
Which is better?
I would prefer a more OO approach instead of fiddling around with strings:
$date = new DateTime($given_date);
$date->setTime(0,0,0);
// echo $date->format('Y-m-d H:i:s');
echo $date->getTimestamp();
Using the static method createFromFormat from DateTime you can force the time-parts to be reset to 0 using '|':
$date = DateTime::createFromFormat('Y-m-d|', $given_date);
echo $date->format('Y-m-d H:i:s');
It is also possible to do it with:
list($y, $m, $d) = explode('-', $given_date);
$midnight = mktime(0, 0, 0, $m, $d, $y);
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
i have string like below "June 1, 2012". i need to find day for the above string.
this value return from wordpress postdate() function.
how can i do this in php?
Can't you parse the date with strtotime?
If so, you can do it like this:
<?php
date('l', strtotime('June 1, 2012'));
?>
<?php echo date('d',strtotime('June 1, 2012')); ?>
You can use a variable instead of the static date string.
$unix_timestamp = strtotime($date);
echo date('l', $unix_timestamp); // displays Monday|Tuesday|Wednesday...
$date = "June 1, 2012";
echo date('l', strtotime($date));
$timestamp= strtotime("June 1, 2012");
$day = date('l', $timestamp);
echo $day;
For more details, read strtotime and date
$date = "June 1, 2012"
$day = date('j', strtotime($date))
You can do different options in the date function to display the day (or other date parts as needed)
Although the answer is easily found, you would be best to research first:
http://uk.php.net/manual/en/function.strtotime.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
Is there a quicker way of creating a date such as:
echo date('Y-m-d', mktime(0, 0, 0, date("m"), date("d")+3, date("Y")));
Thanks if you can help.
How about strtotime():
date('Y-m-d', strtotime('+3 days'));
You will have to look into strtotime(). I'd imagine your final code would look something like this:
$currentDate = strtotime('today');//your date variable goes here
$futureDate = date('Y-m-d', strtotime('+ 2 days', $currentDate));
echo $futureDate;
Live Demo
If you are using PHP version >= 5.2 I strongly suggest you use the new DateTime object. For example like below:
$futureDate = new DateTime("today");
$futureDate->modify("+2 days");
echo $futureDate->format("Y-m-d");
Live Demo