How to increment date with 1 (day/year) in PHP? - php

I have a date stored in an array:
$this->lines['uDate']
The format of the date is not fixed. I can be changed with this:
define('DATETIME_FORMAT', 'y-m-d H:i');
How can I increment my uDate with a certain number of days or years?
My question is related to this one:
increment date by one month
However, in my case the date format in dynamic.
So, can I do this?
$time= $this->lines['uDate'];
$time = date(DATETIME_FORMAT, strtotime("+1 day", $time));
$this->lines['uDate']= $time;

date_add()
and consider changes like:
define(DATETIME_FORMAT, 'y-m-d H:i');
$time = date(DATETIME_FORMAT, strtotime("+1 day", $time));

You can use some simple calculation to do it if you have the timestamp.
$date = strtotime($this->lines['uDate']); //assuming it's not a timestamp\
$date = $date + (60 * 60 * 24); //increase date by 1 day
echo date('d-m-y', $date);
$date = $date + (60 * 60 * 24 * 365); //increase date by a year
echo date('d-m-y', $date);
You can also use the mktime() method to do this : http://php.net/manual/en/function.mktime.php

function add_date($givendate,$day=0,$mth=0,$yr=0)
{
$cd = strtotime($givendate);
$newdate = date('Y-m-d h:i:s', mktime(date('h',$cd),
date('i',$cd), date('s',$cd), date('m',$cd)+$mth,
date('d',$cd)+$day, date('Y',$cd)+$yr));
return $newdate;
}
I have found this in PHP help

another useful way, if you want an object rather than a string:
$date = DateTimeImmutable::createFromFormat('Y-m-d', '2022-01-05'); // just an exemplary date
$date = $date->add(date_interval_create_from_date_string('1 day')); // count up
notable difference:
date_add() changes the original object, while DateTimeImmutable::add() does not and simply returns the new object. Depending on the desired behavior, use one or the other.

Related

How to Display Current date+current time + 1 hour in text box in php [duplicate]

I currently have php returning the current date/time like so:
$now = date("Y-m-d H:m:s");
What I'd like to do is have a new variable $new_time equal $now + $hours, where $hours is a number of hours ranging from 24 to 800.
Any suggestions?
You may use something like the strtotime() function to add something to the current timestamp. $new_time = date("Y-m-d H:i:s", strtotime('+5 hours')).
If you need variables in the function, you must use double quotes then like strtotime("+{$hours} hours"), however better you use strtotime(sprintf("+%d hours", $hours)) then.
An other solution (object-oriented) is to use DateTime::add
Example:
<?php
$now = new DateTime(); //now
echo $now->format('Y-m-d H:i:s'); // 2021-09-11 01:01:55
$hours = 36; // hours amount (integer) you want to add
$modified = (clone $now)->add(new DateInterval("PT{$hours}H")); // use clone to avoid modification of $now object
echo "\n". $modified->format('Y-m-d H:i:s'); // 2021-09-12 13:01:55
Run script
DateTime::add PHP doc
DateInterval::construct PHP doc
You can use strtotime() to achieve this:
$new_time = date("Y-m-d H:i:s", strtotime('+3 hours', $now)); // $now + 3 hours
Correct
You can use strtotime() to achieve this:
$new_time = date("Y-m-d H:i:s", strtotime('+3 hours', strtotime($now))); // $now + 3 hours
You can also use the unix style time to calculate:
$newtime = time() + ($hours * 60 * 60); // hours; 60 mins; 60secs
echo 'Now: '. date('Y-m-d') ."\n";
echo 'Next Week: '. date('Y-m-d', $newtime) ."\n";
Um... your minutes should be corrected... 'i' is for minutes. Not months. :) (I had the same problem for something too.
$now = date("Y-m-d H:i:s");
$new_time = date("Y-m-d H:i:s", strtotime('+3 hours', $now)); // $now + 3 hours
I use this , its working cool.
//set timezone
date_default_timezone_set('GMT');
//set an date and time to work with
$start = '2014-06-01 14:00:00';
//display the converted time
echo date('Y-m-d H:i',strtotime('+1 hour +20 minutes',strtotime($start)));
for add 2 hours to "now"
$date = new DateTime('now +2 hours');
or
$date = date("Y-m-d H:i:s", strtotime('+2 hours', $now)); // as above in example
or
$now = new DateTime();
$now->add(new DateInterval('PT2H')); // as above in example
You can try lib Ouzo goodies, and do this in fluent way:
echo Clock::now()->plusHours($hours)->format("Y-m-d H:m:s");
API's allow multiple operations.
For a given DateTime, you can add days, hours, minutes, etc. Here's some examples:
$now = new \DateTime();
$now->add(new DateInterval('PT24H')); // adds 24 hours
$now->add(new DateInterval('P2D')); // adds 2 days
PHP: DateTime::add - Manual https://www.php.net/manual/fr/datetime.add.php
$to = date('Y-m-d H:i:s'); //"2022-01-09 12:55:46"
$from = date("Y-m-d H:i:s", strtotime("$to -3 hours")); // 2022-01-09 09:55:46
$date_to_be-added="2018-04-11 10:04:46";
$added_date=date("Y-m-d H:i:s",strtotime('+24 hours', strtotime($date_to_be)));
A combination of date() and strtotime() functions will do the trick.
$now = date("Y-m-d H:i:s");
date("Y-m-d H:i:s", strtotime("+1 hours $now"));

Adding two weeks php

I'm trying to add two weeks to sql result that comes back as 16/11/2016. When I do something like
$twoweeks = strtotime($time_db);
$expiry_date = $twoweeks;
$date = strtotime($expiry_date);
$date = strtotime("+14 day", $date);
echo date('d/m/y', $date);
I keeping getting 15/01/70... any ideas?
You made a mistake on line 4 by putting a number variable as a first parameter of strtotime. strtotime expects a string of a valid date/time format as a first parameter otherwise it returns FALSE.
How I think your code should be:
$twoweeks = strtotime($time_db);
$date = strtotime("+ 2 weeks", $twoweeks);
echo date('d/m/y', $date);
Or maybe even:
echo date('d/m/y', strtotime($time_db . ' + 2 weeks'));
You can use
$numberOfWeeks = 2;
$newTime = strtotime($time_db) + ($numberOfWeeks * 60 * 60 * 24 * 7);
or you can do directly in (mysql) select
select date_add( your_column, INTERVAL 2 WEEK) from my_table;
What's happening here is that your 16/11/2016 is day-month-year and the slashes are an issue.
Had your date been 11/16/2016, you would have found that it would have been OK.
You need to convert/replace those to dashes/hyphens.
$time_db = "16/11/2016";
$time_db = str_replace('/', '-', $time_db);
$two_weeks_later = date('d-m-Y',strtotime($time_db . "+14 days"));
// or display as Year-month-day
// $two_weeks_later = date('Y-m-d',strtotime($time_db . "+14 days"));
echo $two_weeks_later;
When working with dates (and times), it's best to use the built-in MySQL date/time functions, rather than storing them as plain text; it's a lot less trouble and much easier when querying.
Reference:
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html
$date = date('Y-m-d H:i:s',time());
$date = strtotime($date);
$date = strtotime("+14 day", $date);
$valuedate= date('Y-m-d H:i:s',$date);
Try this

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/

Date minus 1 year?

I've got a date in this format:
2009-01-01
How do I return the same date but 1 year earlier?
You can use strtotime:
$date = strtotime('2010-01-01 -1 year');
The strtotime function returns a unix timestamp, to get a formatted string you can use date:
echo date('Y-m-d', $date); // echoes '2009-01-01'
Use strtotime() function:
$time = strtotime("-1 year", time());
$date = date("Y-m-d", $time);
Using the DateTime object...
$time = new DateTime('2099-01-01');
$newtime = $time->modify('-1 year')->format('Y-m-d');
Or using now for today
$time = new DateTime('now');
$newtime = $time->modify('-1 year')->format('Y-m-d');
an easiest way which i used and worked well
date('Y-m-d', strtotime('-1 year'));
this worked perfect.. hope this will help someone else too.. :)
On my website, to check if registering people is 18 years old, I simply used the following :
$legalAge = date('Y-m-d', strtotime('-18 year'));
After, only compare the the two dates.
Hope it could help someone.
// set your date here
$mydate = "2009-01-01";
/* strtotime accepts two parameters.
The first parameter tells what it should compute.
The second parameter defines what source date it should use. */
$lastyear = strtotime("-1 year", strtotime($mydate));
// format and display the computed date
echo date("Y-m-d", $lastyear);
Although there are many acceptable answers in response to this question, I don't see any examples of the sub method using the \Datetime object: https://www.php.net/manual/en/datetime.sub.php
So, for reference, you can also use a \DateInterval to modify a \Datetime object:
$date = new \DateTime('2009-01-01');
$date->sub(new \DateInterval('P1Y'));
echo $date->format('Y-m-d');
Which returns:
2008-01-01
For more information about \DateInterval, refer to the documentation: https://www.php.net/manual/en/class.dateinterval.php
You can use the following function to subtract 1 or any years from a date.
function yearstodate($years) {
$now = date("Y-m-d");
$now = explode('-', $now);
$year = $now[0];
$month = $now[1];
$day = $now[2];
$converted_year = $year - $years;
echo $now = $converted_year."-".$month."-".$day;
}
$number_to_subtract = "1";
echo yearstodate($number_to_subtract);
And looking at above examples you can also use the following
$user_age_min = "-"."1";
echo date('Y-m-d', strtotime($user_age_min.'year'));

adding 1 day to a DATETIME format value

In certain situations I want to add 1 day to the value of my DATETIME formatted variable:
$start_date = date('Y-m-d H:i:s', strtotime("{$_GET['start_hours']}:{$_GET['start_minutes']} {$_GET['start_ampm']}"));
What is the best way to do this?
There's more then one way to do this with DateTime which was introduced in PHP 5.2. Unlike using strtotime() this will account for daylight savings time and leap year.
$datetime = new DateTime('2013-01-29');
$datetime->modify('+1 day');
echo $datetime->format('Y-m-d H:i:s');
// Available in PHP 5.3
$datetime = new DateTime('2013-01-29');
$datetime->add(new DateInterval('P1D'));
echo $datetime->format('Y-m-d H:i:s');
// Available in PHP 5.4
echo (new DateTime('2013-01-29'))->add(new DateInterval('P1D'))->format('Y-m-d H:i:s');
// Available in PHP 5.5
$start = new DateTimeImmutable('2013-01-29');
$datetime = $start->modify('+1 day');
echo $datetime->format('Y-m-d H:i:s');
If you want to do this in PHP:
// replace time() with the time stamp you want to add one day to
$startDate = time();
date('Y-m-d H:i:s', strtotime('+1 day', $startDate));
If you want to add the date in MySQL:
-- replace CURRENT_DATE with the date you want to add one day to
SELECT DATE_ADD(CURRENT_DATE, INTERVAL 1 DAY);
The DateTime constructor takes a parameter string time. $time can be different things, it has to respect the datetime format.
There are some valid values as examples :
'now' (the default value)
2017-10-19
2017-10-19 11:59:59
2017-10-19 +1day
So, in your case you can use the following.
$dt = new \DateTime('now +1 day'); //Tomorrow
$dt = new \DateTime('2016-01-01 +1 day'); //2016-01-02
Use strtotime to convert the string to a time stamp
Add a day to it (eg: by adding 86400 seconds (24 * 60 * 60))
eg:
$time = strtotime($myInput);
$newTime = $time + 86400;
If it's only adding 1 day, then using strtotime again is probably overkill.
You can use
$now = new DateTime();
$date = $now->modify('+1 day')->format('Y-m-d H:i:s');
You can use as following.
$start_date = date('Y-m-d H:i:s');
$end_date = date("Y-m-d 23:59:59", strtotime('+3 days', strtotime($start_date)));
You can also set days as constant and use like below.
if (!defined('ADD_DAYS')) define('ADD_DAYS','+3 days');
$end_date = date("Y-m-d 23:59:59", strtotime(ADD_DAYS, strtotime($start_date)));
I suggest start using Zend_Date classes from Zend Framework. I know, its a bit offtopic, but I'll like this way :-)
$date = new Zend_Date();
$date->add('24:00:00', Zend_Date::TIMES);
print $date->get();
Using server request time to Add days. Working as expected.
25/08/19 => 27/09/19
$timestamp = $_SERVER['REQUEST_TIME'];
$dateNow = date('d/m/y', $timestamp);
$newDate = date('d/m/y', strtotime('+2 day', $timestamp));
Here '+2 days' to add any number of days.
One liner !
echo (new \DateTime('2016-01-01 +1 day'))->format('Y-m-d H:i:s');

Categories