PHP - How to count 60 days from the add date - php

Let me know :)
$add_date = date ("Y-m-d H:m:s");
$expiry_date = 'how?';
How to insert into db the $expiry_date for 60 days. mysql format is datetime

Use strtotime():
$start_date = date('Y-m-d H:m:s');
$end_date = date('Y-m-d H:m:s', strtotime("+60 days"));
or more simply:
$end_date = date('Y-m-d H:m:s', time() + 86400 * 60);

A method avoiding time conversions:
$time = date('Y-m-d H:m:s', time()+3600*24*60)
EDIT
However, it may be less readable and the time saved is probably irrelevant. Plus cletus just edited a similar method into his answer

If you are using PHP >= 5.2 I strongly suggest you use the new DateTime object. For example like below:
$add_date = date("Y-m-d H:m:s");
$expiry_date = new DateTime($add_date);
$expiry_date ->modify("+60 days");
echo $expiry_date ->format("Y-m-d H:m:s");
Live Demo

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

PHP Add Days To Date

Alright, so I'm working on a licensing system, and I'm very new to working with PHP.
I'd like to retrieve the number of days a license key has, then add that to the current day.
My code,
$expire = $tags['exp_time']; //EXPIRATION_DATE_DAYS
$expDate = date('Y-m-d H:i:s');
$expDate = strtotime($expDate);
$expDate = $expDate + ((24 * 60 * 60)*($expire));
$expDate = date('Y-m-d H:i:s', $expDate);
I can retrieve the number of days just fine by the way, it's just creating the timestamp in a DATE-TIME format.
Any help/suggestions will greatly be appreciated.
I've also tried,
date_modify(...)
This is easy using DateTime()
$date = new DateTime(); // Create datetime object representing now
$date->modify("+ $expires days"); // Add $expires days to it
$expDate = $date->format("Y-m-d H:i:s"); // Format it
If you're running PHP 5.4+ you can shorten it to a one-liner:
$expDate = (new DateTime())->modify("+ $expires days")->format("Y-m-d H:i:s");
FYI, this code:
$expDate = date('Y-m-d H:i:s');
$expDate = strtotime($expDate);
can be written as:
$expDate = time();
time() returns the current unix timestamp which is all strtotime(date('Y-m-d H:i:s')) does.
Another one-liner, using Carbon:
$expDate = Carbon::now()->addDays($expire)->toDateTimeString();
You can add days to a date using this code:
$ExpDate = date("Y-m-d g:i:s", strtotime("+".$expire." day", date("Y-m-d g:i:s")));
Make sure $expire is a of type int. This code will return the date when the license expires.

how to convert int into time period

I have today date and i want to reduce hours\days from it. i get the "hours to reduce interval" in int that indicate number of days.
I tried something like this:
$today_date = date('Y-m-d H:i:s');
$temp_interval_date = $settings->days_back;
$interval_date = date('H',$temp_interval_date*24);
$final = $temp_interval_date - $interval_date;
My final goal is to get todaydate - interval period in this format
'Y-m-d H:i:s'
I am c# dude :)
Thanks
I'm not entirely clear on what you're asking but I think this is what you're looking for.
$date = new DateTime();
$date->sub(new DatePeriod('P'.$settings->days_back.'D'));
echo $date->format('Y-m-d H:i:s');
You can also do (if you're using PHP 5.2)
$date = new DateTime();
$date->modify('-' . $settings->days_back . ' days'));
echo $date->format('Y-m-d H:i:s');
reference
DateTime()
DatePeriod()
maybe this could be helpful:
echo date('Y-m-d', strtotime('-1 day', date('Y-m-d') ));
Another way to do it
<?php
$temp_interval_date = 2; //in hours (for example =2)
echo date('Y-m-d H:i:s', strtotime('-'.($temp_interval_date*24).' hours',strtotime(date('Y-m-d H:i:s'))));
?>

how do I subtract 24 hour from date time object in PHP

I have the following code:
$now = date("Y-m-d H:m:s");
$date = date("Y-m-d H:m:s", strtotime('-24 hours', $now));
However, now it gives me this error:
A non well formed numeric value encountered in...
why is this?
$date = (new \DateTime())->modify('-24 hours');
or
$date = (new \DateTime())->modify('-1 day');
(The latter takes into account this comment as it is a valid point.)
Should work fine for you here. See http://PHP.net/datetime
$date will be an instance of DateTime, a real DateTime object.
strtotime() expects a unix timestamp (which is number seconds since Jan 01 1970)
$date = date("Y-m-d H:i:s", strtotime('-24 hours', time())); ////time() is default so you do not need to specify.
i would suggest using the datetime library though, since it's a more object oriented approach.
$date = new DateTime(); //date & time of right now. (Like time())
$date->sub(new DateInterval('P1D')); //subtract period of 1 day
The advantage of this is that you can reuse the DateInterval:
$date = new DateTime(); //date & time of right now. (Like time())
$oneDayPeriod = new DateInterval('P1D'); //period of 1 day
$date->sub($oneDayPeriod);
$date->sub($oneDayPeriod); //2 days are subtracted.
$date2 = new DateTime();
$date2->sub($oneDayPeriod); //can use the same period, multiple times.
Carbon (update 2020)
Most popular library for processing DateTimes in PHP is Carbon.
Here you would simply do:
$yesterday = Carbon::now()->subDay();
you can do this in many ways...
echo date('Y-m-d H:i:s',strtotime('-24 hours')); // "i" for minutes with leading zeros
OR
echo date('Y-m-d H:i:s',strtotime('last day')); // 24 hours (1 day)
Output
2013-07-17 10:07:29
Simplest way to sub or add time,
<?php
**#Subtract 24 hours**
$dtSub = new DateTime('- 24 hours');
var_dump($dtSub->format('Y-m-d H:m:s'));
**#Add 24 hours**
$dtAdd = new DateTime('24 hours');
var_dump($dtAdd->format('Y-m-d H:m:s'));die;
?>
This may be helpful for you:
//calculate like this
$date = date("Y-m-d H:m:s", (time()-(60*60*24)));
//check the date
echo $date;
this should work, too
$date = date("Y-m-d H:m:s", strtotime('-24 hours'));
$now = date("Y-m-d H:i:s");
$date = date("Y-m-d H:i:s", strtotime('-24 hours', strtotime($now)));
Add "strtotime" before $now,
and Y-m-d H:m:s replace with Y-m-d H:i:s
You can simply use time() to get the current timestamp.
$date = date("Y-m-d H:m:s", strtotime('-24 hours', time()));
In same code use strtotime() its working.
$now = date("Y-m-d H:i:s");
$date = date("Y-m-d H:i:s", strtotime('-2 hours', strtotime($now)));
Try this :
$now = time();
$date = date("Y-m-d H:m:s", strtotime('-24 hours', $now));
all you have to do is to alter your code to be
$now = strtotime(date("Y-m-d H:m:s"));
$date = date("Y-m-d H:m:s", strtotime('-24 hours', $now));

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