Add one hour to PHP variable with date [duplicate] - php

This question already has answers here:
Adding minutes to date time in PHP
(13 answers)
PHP Date Time Current Time Add Minutes
(13 answers)
Closed 3 years ago.
The title says it all. I have a PHP variable that says: 07/05/2016.
I've tried strtodate, the date function, but nothing seems to be working. How can I now add one hour to this date?

There's quite a few ways to do this with PHP. Here's one using DateTime():
$datetime = new DateTime('07/05/2016');
$datetime->modify('+1 hour');
echo $datetime->format('Y-m-d H:i:s');
In your case you can also just add the literal time since there is no time for that date and midnight is assumed:
echo '07/05/2016 01:00:00';
Just for fun, here are a few more ways to do it:
// Using DateInterval()
$datetime = new DateTime('07/05/2016');
$datetime->add(new DateInterval('PT1H'));
echo $datetime->format('Y-m-d H:i:s');
// As a one-liner
echo (new DateTime('07/05/2016'))->->modify('+1 hour')->format('Y-m-d H:i:s');

You can do it in a simple way
$date = "2019-08-20 17:00:00";
echo date("Y-m-d H:i:s", strtotime("{$date} +1 hour"));
//result 2019-08-20 18:00:00

Here, +1 hour will add one hour.
$date = 07/05/2016
echo date("Y-m-d", strtotime ($date,"+1 hour"));
And for time,
echo date("Y-m-d H:i:s", strtotime ($date,"+1 hour"));

Related

deduction of some hour and minute from NOW() in php [duplicate]

This question already has answers here:
PHP, need to subtract 12 hours and 30 minutes from a DateTime
(10 answers)
Closed 6 years ago.
how can I subtract some hour and minute form the current datetime in php?
example:
current date: 2016-11-22 14:15:50
I need to deduce 1 hr & 25 minute from this ..
how can I do this?
$time = time()-1*60*60-25*60;
echo date('Y-m-d H:i:s', strtotime($time));
Using simply strtotime
echo date("Y-m-d H:i:s",strtotime("-1 hour -25 minutes"));
Using DateTime class
$date = new DateTime("-1 hour -25 minutes");
echo $date->format("Y-m-d H:i:s");
This code might help you :
$newTime = strtotime('-15 minutes'); // as per your question it should be 85
echo 'Time: '.date('Y-m-d H:i:s', $newTime);
Easy solution: with PHP DateTime
$date = new DateTime('2016-11-22 14:15:50');
$date->sub(new DateInterval('P0Y0M0DT1H25M0S'));
echo $date->format('Y-m-d H:i:s') . "\n";
P is period & T is Time.And you know: Y= Year, M= Month, H= Hour, M= Minutes, and S= Second.Just put your required time before these.You can blank DateTime() to get current time.
WORKING DEMO

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

PHP change the date to Y-m-d H:i [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 9 years ago.
What I Require
How can I convert 2013-07-26T10:00:00.000+05:30 to Y-m-d H:i:s format.
I need to get 2013-07-26 10:00:00 from this.
.
What I Tried
$dateTime = DateTime::createFromFormat(DateTime::ISO8601,2013-07-26T10:00:00.000+05:30);
echo $dateTime->format('Y-m-d H:i:s');
AND
$dateTime = DateTime::createFromFormat(DateTime::ISO8601, srtotime(2013-07-26T10:00:00.000+05:30));
echo $dateTime->format('Y-m-d H:i:s');
Note: The date was obtained from google calendar's event start and end date.
Use DateTime
$date = new DateTime('2013-07-26T10:00:00.000+05:30');
echo $date->format('Y-m-d H:i:s');
The second argument for createFromFormat should be the string representing the time, not a timestamp. Try removing strtotime and passing in the string directly.
Alternatively you could do:
echo date('Y-m-d H:i:s', strtotime('2013-07-26T10:00:00.000+05:30'));
Try this code
$datetime="2013-07-26T10:00:00.000+05:30";
$time=strtotime($datetime);
echo date("Y-m-d H:i:s",$time);

php get oldest record and add 1 week [duplicate]

This question already has answers here:
php date format YYYY-MM-DD minus or add one week from now?
(4 answers)
Closed 9 years ago.
I'm looking at making an automated post once a week blog so I can create more postings and then have them go out once a week. I'm having some trouble getting the greatest time and adding 1 week to it.
My database I'm using "datetime".
So the string is in "2013-03-20 09:42:41".
I can get the value of greatest post blog_date, but how do I add 1 week to the string?
date('$blog_date', strtotime("+1 week"));
Thanks for your time ^^
ANSWER WORKS:
$blog_date = date('Y-m-d h:i:s', strtotime("+1 week", strtotime($newest)));
You can use the Datetime object to add a week easily
http://php.net/manual/en/book.datetime.php
$date = new DateTime('2013-03-20 09:42:41');
$date->modify('+1 week');
Try this..
$blog_date = "2013-03-20 09:42:41";
$date2 = strtotime(date("Y-m-d", strtotime($blog_date)) . "+1 week");
echo date('Y-m-d', $date2);
Output
2013-03-27

Print the time an hour ago [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Given a time, how can I find the time one month ago
How can I print an hour ago in PHP using Date?
$date=date("Y-m-d H:i:s");
$time(-1, now);
$result=$date.$time;
So If I wanted to say "John visited last "
Would print
John visited last 20th Feb 2012, 17.26
$date = date('Y-m-d H:i:s', strtotime('-1 hour'));
echo 'John visited last ' . $date;
$date = date("Y-m-d H:i:s", time() - 3600);
time() -> Current timestamp
Time minus 3600 seconds, is the time 1 hour ago. To get the date formatted, you can look here for the options: http://php.net/manual/en/function.date.php
Alternatively you could use the following format:
$date = date("Y-m-d H:i:s", strtotime('-1 hour'));
Though using that method can be a little clunky if you want to remove more specific units of time (Such as one day and 3 hours).
Thats if I've understood what you want to do correctly that is.
I suppose you would be fetching date and time from mysql and the best thing to do is using mysql's DATE_FORMAT function and work out.
Other wise in simple php you could do it like this
$date=date("Y-m-d H:i:s", $time -3600);
Better option is to use strtotime like this one
$date=date("Y-m-d H:i:s", strtotime('-1 hour'));
And get the work done.
Mmm, search the manual the function I used. You are missing something about PHP date/time functions...
// Get the date string for time() - 3600, that is
// the current time minus 3600 seconds (= 1 hour)
$date = date("Y-m-d H:i:s", time() - 3600);
$result = $date;

Categories