21

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

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
TheBlackBenzKid
  • 26,324
  • 41
  • 139
  • 209

4 Answers4

56
$date = date('Y-m-d H:i:s', strtotime('-1 hour'));
echo 'John visited last ' . $date;
Paul
  • 139,544
  • 27
  • 275
  • 264
10
$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.

BenOfTheNorth
  • 2,904
  • 1
  • 20
  • 46
4

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.

abhig10
  • 535
  • 7
  • 24
2

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;
lorenzo-s
  • 16,603
  • 15
  • 54
  • 86