How to add number of days to exact date format [duplicate] - php

This question already has answers here:
Adding days to $Date in PHP
(12 answers)
Closed 8 years ago.
Using PHP how i can do this
$today = date("m/d/y"); // showing today
Output 11/18/13 will be saved in mysql as start date
Okay so if i like to add to this start date 30 days ... 60 days ... 90 days or any number of days to be add to $today and results be still in format m/d/y
Example may explain more what i mean
$today = date("m/d/y"); // 11/18/13
then add 10 days so $expired should be $today + 10; in days and results should be 11/28/13
~ thanks

Use DateTime::add() (PHP 5.3)
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P10D'));
echo $date->format('Y-m-d') . "\n";
?>

Try something like this should work for you
<?php
$Date = date("m/d/y");
echo date('m/d/y', strtotime($Date. ' + 10 days'));
?>

Use this code:
$today = date("m/d/y");
echo date('m/d/y', strtotime($today. ' +10 day'));
Out put will be 11/28/13 as you desiring.

Related

How to add one day to a date which is given as 'month-day'? [duplicate]

This question already has answers here:
Adding one day to a date
(13 answers)
Closed 5 years ago.
I have a date which is:
$firstDay = "'" . date('m-d', easter_date(date('Y'))) . "'";
Output of $firstDay is: '04-16'. Now I need to create a second variable and add 1 day to the $firstDay variable. How should I do that? I red that I can do with $date->modify(), I'm trying like so:
$secondDay = $firstEasterDay->modify('+1 day');
But this doesn't work and I got an error.
Thanks for any help.
<?php
$date = "04-15-2013";
$date = strtotime($date);
$date = strtotime("+1 day", $date);
echo date('m-d-Y', $date);?>
Try This

How to set a date of n number of days ago in php [duplicate]

This question already has answers here:
Subtracting days, months or years from date using php [closed]
(4 answers)
Closed 5 years ago.
I have a php variable:
$datevar = date("Y-m-d");
Which shows me the CURRENT date in the specified format.
What I want is the date from 7 days ago for CURRENT date in that format.
I have tried:
$datevar = $datevar - 7;
and
$datevar = date("Y-m-d") - 7 ;
But they both cause an error.
I refer to this solution: https://stackoverflow.com/a/3727821/7454754
So in your example this would be something like
<?php
$today = date("Y-m-d");
$newDate = date("Y-m-d", strtotime($today . " - 7 days"));
?>
Try the code below, that should work:
$date = date("Y-m-d");// current date
$date = strtotime(date("Y-m-d", strtotime($date)) . " -1 week");
http://php.net/manual/en/function.strtotime.php
You can do it like this. Here we are using DateTime and DateInterval
Try this code snippet here
<?php
$datevar = date("Y-m-d");//your date
$dateTimeObj=new DateTime($datevar);
$dateTimeObj->sub(new DateInterval("P7D"));//subtracting 7 days
echo $dateTimeObj->format("Y-m-d");

How do count forward X number of days into future in PHP? [duplicate]

This question already has answers here:
Add number of days to a date
(20 answers)
Closed 8 years ago.
I need to be able to count forward X number of dates into the future in PHP. For example, 14 days from today is what date? What routines in PHP can be used for this? Thanks!
This is easy with date() and strtotime():
echo date('Y-m-d', strtotime('+14 days'));
You can also use DateTime:
$date = new DateTime();
$date->modify('+14 days');
echo $date->format('Y-m-d');
Or:
$date = new DateTime('+14 days');
echo $date->format('Y-m-d');
Or as a one-liner:
echo (new DateTime('+14 days'))->format('Y-m-d');
Or with DateInterval:
$date = new DateTime();
$date->add(new DateInterval('P14D'));
echo $date->format('Y-m-d');

define var today and yesterday [duplicate]

This question already has answers here:
Get timestamp of today and yesterday in php
(9 answers)
Closed 8 years ago.
I have
$today = date("Y-m-d");
and I like to define also $yesterday
I tried to do
$yesterday=($today-1)
But it doesn't work
Just try with:
$yesterday = date('Y-m-d', strtotime('yesterday'));
You could use something like:
date("F j, Y", time() - 60 * 60 * 24);
You can also use DateTime:
$date = new DateTime('');
$date->modify('-1 day');
echo $date->format('Y-m-d') . "\n";

How many days left till any date? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How many days until XXX date?
Currently I am using this code to determine how many days left till an expected day. But this code shows unexpected result. For example if $last_date is 26 December 2012, then I will get 0 day(s) left. But it should be 1 day(s) left. I think my problem is only with floor() function. Isn't it?
$timezone = "Asia/Dhaka";
if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
$now = time();
$last_date = strtotime("$year-$month-$day");
$datediff = $last_date - $now;
$day_left=floor($datediff/(60*60*24));
echo "$day_left day(s) left.";
N:B: My timezone is +6 GMT, I mean Asia/Dhaka.
As per the PHP documentation:
<?php
$year = '2012';
$month = '12';
$day = '26';
$current_date = new DateTime(date('Y-m-d'), new DateTimeZone('Asia/Dhaka'));
$end_date = new DateTime("$year-$month-$day", new DateTimeZone('Asia/Dhaka'));
$interval = $current_date->diff($end_date);
echo $interval->format('%a day(s)');
?>

Categories