Simplest way to increment a date in PHP? - php

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

Related

Add number of days of an existing days

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;
}

How to get time from datetime

I have datetime 30-12-1899 9:25:52 AM here needed only time that is 9:25:52 AM .this time i wanted to insert in mysql database and this field has data type time.
my code is :
<?php
$date = "30-12-1899 9:25:52 AM";
$date = strtotime($date);
echo date('H:i:s', $date);
?>
when execute it returns:
01:00:00
i am not getting where is the problem.Can any one help me on this.
thank you.
Do the OOP way
<?php
$date1="30-12-1899 9:25:52 AM";
$format = 'd-m-Y H:i:s A';
$date = DateTime::createFromFormat($format, $date1);
echo $date->format('H:i:s A') . "\n";
$s = '30-12-1899 9:25:52 AM';
$dt = new DateTime($s);
$date = $dt->format('d-m-Y');
$time = $dt->format('h:i:s A');
echo $time;
see details here http://docs.php.net/class.datetime
actually the code does exactly what you want
<?php
$date = "30-12-1899 9:25:52 AM";
$date = strtotime($date);
echo date('H:i:s', $date);
?>
demo
Can you try this,
$date = "1899-12-30 9:25:52 AM";
echo $start_time = date("h:i:s A", strtotime($date));
OR
$date = "30-12-1899 9:25:52 AM";
$dtime = new DateTime($date);
echo $dtime->format("h:i:s A");
You get 01:00:00 as result, because on your version of PHP, strtotime() function returns invalid integer. More likely value false is returned, and when you format false via date() function, you will get 1970-01-01 00:00:00. You get time 01:00:00 because you have timezone offset, you are probably in UTC+1 timezone.
Here you can see, that your code will not work correctly on PHP version bellow 5.2.6, even on x64 machines, and results will be unreliable.
Best option would be to upgrade your PHP version (or change webhosting). But if you do not have this options, you can use string functions to break and concatenate date & time parts, like this:
$date = "30-12-1899 9:25:52 AM";
sscanf($date, "%d-%d-%d %d:%d:%d %s", $d, $m, $Y, $g, $i, $s, $A);
$h = $A == 'AM' ? $g : $g + 12;
echo "$Y-$m-$d $h:$i:$s"; # 1899-12-30 9:25:52
echo "$h:$i:$s"; # 9:25:52
You can try with explode like
$date = "30-12-1899 9:25:52 AM";
$date_arr = explode(' ',$date);
echo $date_arr[1];
This can be alternative
<?php
$date = "30-12-1899 9:25:52 AM";
$date = explode(' ',$date);
echo date[1].' '.date[2];
?>

Adding days to date in php

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

PHP, Get tomorrows date from date

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.

Add days to a date in PHP

Is there any php function available where I can add days to a date to make up another date? For example, I have a date in the following format:
27-December-2011
If I add 7 to the above, it should give:
03-January-2012.
Many thanks
Try this
$add_days = 7;
$date = date('Y-m-d',strtotime($date) + (24*3600*$add_days));
Look at this simple snippet
$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");
You can use the add method of DateTime. Anyway this solution works for php version >= 5.3
date('Y-m-d', strtotime('+6 days', strtotime($original_date)));
Actually it's easier than all that.
$some_var = date("Y-m-d",strtotime("+7 day"))
You can use a variable instead of the string, of course. It will be great if the people answering the questions, won't complicate things. Less code, means less time to waste on the server ;).
$date = new DateTime('27-December-2011');
$date->add(new DateInterval('P7D'));
echo $date->format('d-F-Y') . "\n";
Change the format string to be whatever you want. (See the documentation for date()).
$registered = $udata->user_registered;
$registered = date( "d m Y", strtotime( $registered ));
$challanexpiry = explode(' ', $registered);
$day = $challanexpiry[0];
$month = $challanexpiry[1];
$year = $challanexpiry[2];
$day = $day+10;
$bankchallanexpiry = $day . " " . $month . " " . $year;

Categories