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 :)
Related
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
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"));
This question already has answers here:
PHP DateTime microseconds always returns 0
(18 answers)
Closed 6 years ago.
I have a timestamp
1457459333506 (Tue, 08 Mar 2016 17:48:53 GMT)
This should be Unix timestamp with milliseconds. I want to write this as a string, so I'm using
$dt = new DateTime("#$unixTimestamp");
echo date('Y-m-d H:i:s.u', $unixTimestamp) . "<br>";
But the output is 2016-06-08 22:09:22.000000
This is obviously wrong, and has no milisecond precision. So I've tried
echo date('Y-m-d H:i:s.u', $unixTimestamp / 1000) . "<br>";
Which outputs as 2016-03-08 17:48:53.000000 (correct, but also has no milisecond precision).
How can I get this to output correctly as: 2016-03-08 17:48:53.506 ?
As simple as
$unixTimestamp = 1457459333506;
$dt = DateTime::createFromFormat("U.u", $unixTimestamp / 1000);
var_dump($dt);
with a DateTime object
Demo
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I have this function that runs on the server and I want a different output based on the day of the week, Saturday and Sunday should give weekend message and different for the weekdays.
$timein = date("Y-m-d h:i:s");
$dw = date("D", $timein);
$tm = date("e", $timein);
echo "Current Date: ".$timein."<br>";
echo "Day of the week: ".$dw."<br>";
echo "Timezone: ".$tm."<br>";
and this is the output:
Current Date: 2015-05-15 06:07:12 Day of the week: Wed
Timezone: America/Denver
We are Friday and I was expecting Fri, I was using w instead of D but I always was getting 3 in the results.
You need to use strtotime function to convert it into a Unix timestamp. Just update your code into.
$dw = date("D", strtotime($timein));
$tm = date("e", strtotime($timein));
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;