1 hour ago in php

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

19.3k7 gold badges69 silver badges100 bronze badges

asked Feb 20, 2012 at 17:29

TheBlackBenzKidTheBlackBenzKid

25.5k39 gold badges138 silver badges207 bronze badges

1

$date = date['Y-m-d H:i:s', strtotime['-1 hour']];
echo 'John visited last ' . $date;

answered Feb 20, 2012 at 17:31

0

$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: //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.

answered Feb 20, 2012 at 17:32

BenOfTheNorthBenOfTheNorth

2,8841 gold badge19 silver badges46 bronze badges

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.

answered Feb 20, 2012 at 17:40

abhig10abhig10

5156 silver badges24 bronze badges

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;

answered Feb 20, 2012 at 17:33

lorenzo-slorenzo-s

16.2k15 gold badges48 silver badges85 bronze badges

Not the answer you're looking for? Browse other questions tagged php string date time date-arithmetic or ask your own question.

PHPServer Side ProgrammingProgramming


Kickstart HTML, CSS and PHP: Build a Responsive Website

Featured

59 Lectures 8.5 hours

Ogbemudia Terry Osayawe

More Detail

To print time an hour ago, you need to subtract -1 hour. Use the strtotime[] method and set the subtraction value in it i.e. -1 for 1 hour ago time, -2 for 2 hour ago time, etc.

Let’s say the current time is 

18:42:22

Example

The PHP code is as follows

 Live Demo





Output

This will produce the following output

One hour ago, the date = 2020-09-26 17:42:22

AmitDiwan

Updated on 13-Oct-2020 08:04:40

  • Related Questions & Answers
  • PHP program to convert a given timestamp into time ago
  • How to convert 12-hour time scale to 24-hour time in R?
  • 24-hour time in Python
  • Converting 12 hour format time to 24 hour format in JavaScript
  • PHP print keys from an object?
  • C# program to convert time from 12 hour to 24 hour format
  • Python program to convert time from 12 hour to 24 hour format
  • C++ program to convert time from 12 hour to 24 hour format
  • Convert time from 24 hour clock to 12 hour clock format in C++
  • Set the hour for a specified date according to universal time?
  • Java Program to display Time in 12-hour format
  • Java Program to display time in 24-hour format
  • Python Pandas - Extract the hour from the DateTimeIndex with specific time series frequency
  • Python Pandas - Format the Period object and display the Time with 24-Hour format
  • Insert current time minus 1 hour to already inserted date-time records in MYSQL

Previous Page Print Page Next Page  

Advertisements

How to get 1 hour before time in PHP?

$date = date["Y-m-d H:i:s", strtotime['-1 hour']];

How to add 1 hour to current time in PHP?

$time = strtotime['+1 hour']; strtotime['+1 hour', $time]; $time = date['H:i', strtotime['+1 hour']];

How to get hours from time in PHP?

Use the Datetime class [PHP 5.3 or higher]. Show activity on this post. By hours I'm assuming you mean if the time is 8PM or 20:00 hours like it is in your time string then... $date = "2011-07-26 20:05:00"; $date = strtotime[$date]; echo date['H', $date];

How to convert timestamp to time ago in PHP?

$str . echo to_time_ago[ time[] - 5];

Chủ Đề