I read about this but does not working for me. Here is my code:
$today = date_create()->format("d/m/Y"); // Today is 25/04/2013
$num_days = GetNumberOfdays();
$end_date = date("d/m/Y", strtotime($today . " + $num_days days"));
The value that I get from $end_date is 31/12/1969. What am I doing wrong?
Try this instead:
$end_date = date("d/m/Y", strtotime("+ $num_days days", time()));
EDIT: I changed the $today variable to just time() which is essentially getting you the same information if you're just looking for today's date.
From what it looks like you're trying to do, you don't even need $today (as it defaults to now if date is not supplied), so you could just do eg:
$end_date = date("d/m/Y", strtotime("+ 5 days"));
echo $end_date;
result would be
30/04/2013
if you want to provide a date, you need the parameters the other way round, as per the manual:
strtotime ( string $time [, int $now = time() ] )
date_create() return a DateTime object.
You could use DateTime::modify method.
$date = new \DateTime(); // Defaults to Today
$num_days = 123;
$date->add(
new \DateInterval('P' . $num_days . 'D')
);
echo $date->format('d-M-Y');
$today = date_create()->format("d/m/Y"); // Today is 25/04/2013
$num_days = date_create()->format("d");
echo $end_date = date("d/m/Y", strtotime(" + $num_days days"));
<?
// note change of $today format
$today = date_create()->format("d-m-Y"); // Today is 25-04-2013
$num_days = GetNumberOfdays();
$end_date = date("d/m/Y", strtotime("+" . $num_days . " days", strtotime($today)));
?>
Related
I have a problem and I don't understand where it is :
So If I do :
$end_date = date('Y-m-d H:i:s',strtotime("+ $frequency days")); --> it works
If I do :
$end = $o_user->end;
$o_user->end = date($end, strtotime("+ $frequency days")); ---> not work
I tested and the 2 dates have the format : Y-m-d H:i:s
Where is my error ? Please help me. Thx in advance
Date's first param is the format, not an another date.
It should be something like this:
$o_user->end = date("Y-m-d H:i:s", strtotime($end . " +$frequency days"));
Maybe you just want to do
$o_user->end->modify("+ $frequency days");
It's even more readable and compact.
BTW your error is that date() function expect as first parameter a string (the date format)
Change to $o_user->end = date('Y-m-d H:i:s', strtotime($end, "+". $frequency. "days"));
You can use below code
$i_frequency = 4;
$end = '2016-05-23 10:48:42';
echo "==" . date('Y-m-d', strtotime("+$i_frequency days", strtotime($end)));
OR
$i_frequency = 4;
$end = '2016-05-23 10:48:42';
echo "==" . addDate($end, $i_frequency);
function addDate($date, $day)//add days
{
$sum = strtotime(date("Y-m-d", strtotime("$date")) . " +$day days");
$dateTo = date('Y-m-d', $sum);
return $dateTo;
}
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/
I have a PHP date in the form of 2013-01-22 and I want to get tomorrows date in the same format, so for example 2013-01-23.
How is this possible with PHP?
Use DateTime
$datetime = new DateTime('tomorrow');
echo $datetime->format('Y-m-d H:i:s');
Or:
$datetime = new DateTime('2013-01-22');
$datetime->modify('+1 day');
echo $datetime->format('Y-m-d H:i:s');
Or:
$datetime = new DateTime('2013-01-22');
$datetime->add(new DateInterval("P1D"));
echo $datetime->format('Y-m-d H:i:s');
Or in PHP 5.4+:
echo (new DateTime('2013-01-22'))->add(new DateInterval("P1D"))
->format('Y-m-d H:i:s');
$tomorrow = date("Y-m-d", strtotime('tomorrow'));
or
$tomorrow = date("Y-m-d", strtotime("+1 day"));
Help Link: STRTOTIME()
Since you tagged this with strtotime, you can use it with the +1 day modifier like so:
$tomorrow_timestamp = strtotime('+1 day', strtotime('2013-01-22'));
That said, it's a much better solution to use DateTime.
<? php
//1 Day = 24*60*60 = 86400
echo date("d-m-Y", time()+86400);
?>
echo date ('Y-m-d',strtotime('+1 day', strtotime($your_date)));
Use DateTime:
To get tomorrow from now :
$d = new DateTime('+1day');
$tomorrow = $d->format('d/m/Y h.i.s');
echo $tomorrow;
Results : 28/06/2017 08.13.20
To get tomorrow from a date :
$d = new DateTime('2017/06/10 08.16.35 +1day')
$tomorrow = $d->format('d/m/Y h.i.s');
echo $tomorrow;
Results : 11/06/2017 08.16.35
Hope it helps!
/**
* get tomorrow's date in the format requested, default to Y-m-d for MySQL (e.g. 2013-01-04)
*
* #param string
*
* #return string
*/
public static function getTomorrowsDate($format = 'Y-m-d')
{
$date = new DateTime();
$date->add(DateInterval::createFromDateString('tomorrow'));
return $date->format($format);
}
By strange it can seem it works perfectly fine: date_create( '2016-02-01 + 1 day' );
echo date_create( $your_date . ' + 1 day' )->format( 'Y-m-d' );
Should do it
here's working function
function plus_one_day($date){
$date2 = formatDate4db($date);
$date1 = str_replace('-', '/', $date2);
$tomorrow = date('Y-m-d',strtotime($date1 . "+1 days"));
return $tomorrow; }
$date = '2013-01-22';
$time = strtotime($date) + 86400;
echo date('Y-m-d', $time);
Where 86400 is the # of seconds in a day.
I have a date returned as part of a MySQL query in the form 2010-09-17.
I would like to set the variables $Date2 to $Date5 as follows:
$Date2 = $Date + 1
$Date3 = $Date + 2
etc., so that it returns 2010-09-18, 2010-09-19, etc.
I have tried
date('Y-m-d', strtotime($Date. ' + 1 day'))
but this gives me the date before $Date.
What is the correct way to get my Dates in the format form 'Y-m-d' so that they may be used in another query?
All you have to do is use days instead of day like this:
<?php
$Date = "2010-09-17";
echo date('Y-m-d', strtotime($Date. ' + 1 days'));
echo date('Y-m-d', strtotime($Date. ' + 2 days'));
?>
And it outputs correctly:
2010-09-18
2010-09-19
If you're using PHP 5.3, you can use a DateTime object and its add method:
$Date1 = '2010-09-17';
$date = new DateTime($Date1);
$date->add(new DateInterval('P1D')); // P1D means a period of 1 day
$Date2 = $date->format('Y-m-d');
Take a look at the DateInterval constructor manual page to see how to construct other periods to add to your date (2 days would be 'P2D', 3 would be 'P3D', and so on).
Without PHP 5.3, you should be able to use strtotime the way you did it (I've tested it and it works in both 5.1.6 and 5.2.10):
$Date1 = '2010-09-17';
$Date2 = date('Y-m-d', strtotime($Date1 . " + 1 day"));
// var_dump($Date2) returns "2010-09-18"
From PHP 5.2 on you can use modify with a DateTime object:
http://php.net/manual/en/datetime.modify.php
$Date1 = '2010-09-17';
$date = new DateTime($Date1);
$date->modify('+1 day');
$Date2 = $date->format('Y-m-d');
Be careful when adding months... (and to a lesser extent, years)
Here is a small snippet to demonstrate the date modifications:
$date = date("Y-m-d");
//increment 2 days
$mod_date = strtotime($date."+ 2 days");
echo date("Y-m-d",$mod_date) . "\n";
//decrement 2 days
$mod_date = strtotime($date."- 2 days");
echo date("Y-m-d",$mod_date) . "\n";
//increment 1 month
$mod_date = strtotime($date."+ 1 months");
echo date("Y-m-d",$mod_date) . "\n";
//increment 1 year
$mod_date = strtotime($date."+ 1 years");
echo date("Y-m-d",$mod_date) . "\n";
You can also use the following format
strtotime("-3 days", time());
strtotime("+1 day", strtotime($date));
You can stack changes this way:
strtotime("+1 day", strtotime("+1 year", strtotime($date)));
Note the difference between this approach and the one in other answers: instead of concatenating the values +1 day and <timestamp>, you can just pass in the timestamp as the second parameter of strtotime.
Here has an easy way to solve this.
<?php
$date = "2015-11-17";
echo date('Y-m-d', strtotime($date. ' + 5 days'));
?>
Output will be:
2015-11-22
Solution has found from here - How to Add Days to Date in PHP
Using a variable for Number of days
$myDate = "2014-01-16";
$nDays = 16;
$newDate = strtotime($myDate . '+ '.$nDays.' days');
echo new Date('d/m/Y', $newDate); //format new date
Here is the simplest solution to your query
$date=date_create("2013-03-15"); // or your date string
date_add($date,date_interval_create_from_date_string("40 days"));// add number of days
echo date_format($date,"Y-m-d"); //set date format of the result
This works. You can use it for days, months, seconds and reformat the date as you require
public function reformatDate($date, $difference_str, $return_format)
{
return date($return_format, strtotime($date. ' ' . $difference_str));
}
Examples
echo $this->reformatDate('2021-10-8', '+ 15 minutes', 'Y-m-d H:i:s');
echo $this->reformatDate('2021-10-8', '+ 1 hour', 'Y-m-d H:i:s');
echo $this->reformatDate('2021-10-8', '+ 1 day', 'Y-m-d H:i:s');
To add a certain number of days to a date, use the following function.
function add_days_to_date($date1,$number_of_days){
/*
//$date1 is a string representing a date such as '2021-04-17 14:34:05'
//$date1 =date('Y-m-d H:i:s');
// function date without a secrod argument returns the current datetime as a string in the specified format
*/
$str =' + '. $number_of_days. ' days';
$date2= date('Y-m-d H:i:s', strtotime($date1. $str));
return $date2; //$date2 is a string
}//[end function]
All have to use bellow code:
$nday = time() + ( 24 * 60 * 60);
echo 'Now: '. date('Y-m-d') ."\n";
echo 'Next Day: '. date('Y-m-d', $nday) ."\n";
Another option is to convert your date string into a timestamp and then add the appropriate number of seconds to it.
$datetime_string = '2022-05-12 12:56:45';
$days_to_add = 1;
$new_timestamp = strtotime($datetime_string) + ($days_to_add * 60 * 60 * 24);
After which, you can use one of PHP's various date functions to turn the timestamp into a date object or format it into a human-readable string.
$new_datetime_string = date('Y-m-d H:i:s', $new_timestamp);
Say I have a string coming in, "2007-02-28", what's the simplest code I could write to turn that into "2007-03-01"? Right now I'm just using strtotime(), then adding 24*60*60, then using date(), but just wondering if there is a cleaner, simpler, or more clever way of doing it.
A clean way is to use strtotime()
$date = strtotime("+1 day", strtotime("2007-02-28"));
echo date("Y-m-d", $date);
Will give you the 2007-03-01
It's cleaner and simpler to add 86400. :)
The high-tech way is to do:
$date = new DateTime($input_date);
$date->modify('+1 day');
echo $date->format('Y-m-d');
but that's really only remotely worthwhile if you're doing, say, a sequence of transformations on the date, rather than just finding tomorrow.
You can do the addition right inside strtotime, e.g.
$today="2007-02-28";
$nextday=strftime("%Y-%m-%d", strtotime("$today +1 day"));
Another way is to use function mktime(). It is very useful function...
$date = "2007-02-28";
list($y,$m,$d)=explode('-',$date);
$date2 = Date("Y-m-d", mktime(0,0,0,$m,$d+1,$y));
but I think strtotime() is better in that situation...
The simplest way...
echo date('Y-m-d',strtotime("+1 day")); //from today
OR from specified date...
echo date('Y-m-d',strtotime("+1 day", strtotime('2007-02-28')));
Hello you can try this below especially if you are french
$date = date('l j F Y');
#increment the date
$date2 = date('l j F Y', strtotime("+7 day"));
to translate in french you can use the setlocale() function or the function below :
function fr_date($date){
$date = explode(' ', $date);
$date = str_replace('Monday','Lundi',$date);
$date = str_replace('Tuesday','Mardi',$date);
$date = str_replace('Wednesday','Mercredi',$date);
$date = str_replace('Thursday','Jeudi',$date);
$date = str_replace('Friday','Vendredi',$date);
$date = str_replace('Saturday','Samedi',$date);
$date = str_replace('Sunday','Dimanche',$date);
$date = str_replace('January','Janvier',$date);
$date = str_replace('February','Février',$date);
$date = str_replace('March','Mars',$date);
$date = str_replace('April','Avril',$date);
$date = str_replace('May','Mai',$date);
$date = str_replace('June','Juin',$date);
$date = str_replace('July','Juillet',$date);
$date = str_replace('August','Août',$date);
$date = str_replace('September','Septembre',$date);
$date = str_replace('October','Octobre',$date);
$date = str_replace('November','Novembre',$date);
$date = str_replace('December','Décembre',$date);
$date = implode(' ',$date);
return $date;
}
$your_date = strtotime("1month", strtotime(date("Y-m-d")));
$new_date = date("Y-m-d", $your_date++);
use strtotime() With date Formate
echo date('Y-m-d', strtotime('2007-02-28' . ' +1 day'));
$early_start_date = date2sql($_POST['early_leave_date']);
$date = new DateTime($early_start_date);
$date->modify('+1 day');
$date_a = new DateTime($early_start_date . ' ' . $_POST['start_hr'] . ':' . $_POST['start_mm']);
$date_b = new DateTime($date->format('Y-m-d') . ' ' . $_POST['end_hr'] . ':' . $_POST['end_mm']);
$interval = date_diff($date_a, $date_b);