Print the time an hour ago [duplicate] - php

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;

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

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 :)

Difference between dates [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Difference between dates
How to calculate the date difference between 2 dates using php
So, I have two dates. For instance, 2010-09-24 and 2010-09-25. I want to check if between those two dates the difference is 1 day.
I don't need to get a value of 86400 which means one day in seconds.
Don't forget that it can be 28, 28, 29, 30, 31 days in month.
Thanks.
What I have right now, but it doesn't work when there is difference between months:
$strto = strtotime($date);
$d1 = date('d', $strto);
$d2 = date('d', time());
echo $d2- $d1;
You can use strtotime to get the number of seconds in between
echo abs(strtotime('2010-09-24') - strtotime('2010-09-25'));
Don't use the day value - (eg date('d', ...)) - leave it as an integer (the result of strtotime()). Then subtract those dates, and then get the floor(difference / 86400).
Like so:
$dt = strtotime($date);
echo floor(abs(time() - $dt) / 86400);
You can do this nicely with the DateTime class if you have PHP 5.3:
<?php
$datetime1 = new DateTime('2010-09-25');
$datetime2 = new DateTime('2010-09-26');
$interval = $datetime1->diff($datetime2);
$intervaldays = (int) $interval->format('%R%d'); // %R signs the result +/-
This is probably less efficient than using strtotime methods, but it is very readable.
Why are you using date('d'... which returns the day of the month?
strtotime will create a UNIX-timestamp which is exactly what time() returns, so abs(time() - strtotime($date)) should already do the job. This way you don't have to worry how many days a month has as you're only working with timestamps.
This will get you the number of (complete) days:
floor( abs(time() - strtotime($date)) / 86400 )

get current date and date after two months in php

this is a very lame question but i m not able to find this one.
How to get today's date and date after two months..
format is month-date-year (numerical.)
You can use the strtotime() function :
$today = time();
$twoMonthsLater = strtotime("+2 months", $today);
// If what you really want is exactly 60 days later, then
$sixtyDaysLater = strtotime("+60 days", $today);
// ...or 8 weeks later :
$eightWeeksLater = strtotime("+8 weeks", $today);
In any case, the resulting new timestamps can then be converted to month-date-year :
echo 'Today is : ' . date('m-d-Y', $today);
echo 'Two months later will be : ' . date('m-d-Y', $twoMonthsLater);
** UPDATE **
From the PHP manual
Note: Please keep in mind that these functions are dependent on the locale settings of your server. Make sure to take daylight saving time (use e.g. $date = strtotime('+7 days', $date) and not $date += 7*24*60*60) and leap years into consideration when working with these functions.
Just thought I should mention it...
There are a couple of ways you could go about it - the first one would be to do something like this:
echo $currentdate = date("Y-m-d H:i:s",time());
echo $after60days = date('Y-m-d H:i:s', time() + 60 * 60 * 24 * 60);
Basically, you take the current timestamp, expressed in seconds, and add 60 * 60 * 24 * 60, which is the amount of seconds in two months.
Another way to do it, which is my preferred way and how I would do it, is this:
$after60days = strtotime("+60 days");
The outcome will be exactly the same, $after60days will have a value equal to the timestamp of the day exactly two month from now, but it uses PHP's own strtotime() function.
Of course, if you need to output a date in a format that easy to read for humans, you can do something like this:
echo date('Y-m-d H:i:s',$after60days);
Today:
date('m-d-Y', time());
Two months from now:
date('m-d-Y', time() + (86400 * 60));

Categories