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

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

Related

Add one hour to PHP variable with date [duplicate]

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

How to get current date in certain format with PHP? [duplicate]

This question already has answers here:
PHP, How to get current date in certain format [duplicate]
(3 answers)
Closed 6 years ago.
I want to get current date and time in certain format .
and current timestamp minus 15 minutes in certain format .
How do I do this in PHP?
Please try following code :
echo "current time: " .date('Y-m-d h:i:s');
echo "<br>current timestamp minus 15 minutes :". date('Y-m-d H:i:s', strtotime('-15 minutes'));
You could get current datetime using php date() function, for your format you could use following code -
date("Y-m-d H:i:s")
for current time stamp minus 15 minutes you could use time() function -echo time() - (15 * 60)
to get exact time of minus 15 minutes in datetime format you could use this code - echo date("Y-m-d H:i:s", time() - (15 * 60));
Current time:
date_default_timezone_set('Australia/Melbourne');
$date = date('m-d-Y h:i:s a', time());
-minus 15 minutes:
echo date('m-d-Y h:i:s', strtotime('-15 minutes'));
Try and google next time :)

How to calculate the hour,min between two date time? [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 9 years ago.
I have two date's
$date1 = "2014-02-11 04:04:26 AM"
$date2 = "2014-02-11 05:36:56 AM"
I want to calculate the difference and display it as follows
1 hour 32 minutes
Make use of DateTime::diff of the DateTime Class
<?php
$datetime1 = new DateTime('2014-02-11 04:04:26 AM');
$datetime2 = new DateTime('2014-02-11 05:36:56 AM');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%h')." Hours ".$interval->format('%i')." Minutes";
OUTPUT :
1 Hours 32 Minutes
Simply convert both dates to timestamp if dont want to do it in complex way...
Something like this
$dateDiff = intval((strtotime($date1)-strtotime($date2))/60);
$hours = intval($dateDiff/60);
$minutes = $dateDiff%60;
and there you go...
Thank you...

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;

PHP, need to subtract 12 hours and 30 minutes from a DateTime

I have a PHP DateTime variable.
How can I reduce or subtract 12hours and 30 minutes from this date in at PHP runtime?
Subtract 12 Hours and 30 minutes from a DateTime in PHP:
$date = new DateTime();
$tosub = new DateInterval('PT12H30M');
$date->sub($tosub);
The P stands for Period. The T stands for Timespan.
See DateTime, DateTime::sub, and DateInterval in the PHP manual. You'll have to set the DateTime to the appropriate date and time, of course.
Try with:
$date = new DateTime('Sat, 30 Apr 2011 05:00:00 -0400');
echo $date->format('Y-m-d H:i:s') . "\n";
$date->sub(new DateInterval('PT12H30M'));
echo $date->format('Y-m-d H:i:s') . "\n";
//Result
2011-04-30 05:00:00
2011-04-29 16:30:00
Try strtotime() function:
$source_timestamp=strtotime("Sat, 30 Apr 2011 05:00:00 -0400");
$new_timestamp=strtotime("-12 hour 30 minute", $source_timestamp);
print date('r', $new_timestamp);
Maybe it will be useful for some cases
$date = new DateTime();
$date->modify('-12 hours -30 minutes');
echo $date->format('H:i:s');
try using this instead
//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)));
If you are not so familiar with the spec of DateInterval like PT12H30M you can proceed with more human readable way using DateInterval::createFromDateString as follows :
$date = new DateTime();
$interval = DateInterval::createFromDateString('12 hour 30 minute');
$date->sub($interval);
Or with direct interval in sub function like below :
$date = new DateTime();
$date->sub(DateInterval::createFromDateString('12 hour 30 minute'));
Store it in a DateTime object and then use the DateTime::sub method to subtract the timespan.
I used in one line, for 12 hours only, and just as an hour display
$date = new DateTime(); $date->modify('-12 hours'); echo $date->format('H')-0;
I used the -0 since sometimes it put a 0 in front of the digit unless I done that, strange.
Here is detailed description of date function,
Using simply strtotime
echo date("Y-m-d H:i:s",strtotime("-12 hour -30 minutes"));
Using DateTime class
$date = new DateTime("-12 hour -30 minutes");
echo $date->format("Y-m-d H:i:s");

Categories