SagiLow
SagiLow

Reputation: 6029

Formatting time() in PHP

I'm using a database where in the table I have the time after it was saved using time()
Is there any way formatting it to human readable way (date and time)?

Thanks

Upvotes: 2

Views: 1146

Answers (5)

Christine Rohacz
Christine Rohacz

Reputation: 1

After querying the database for the timestamp, pass it through the function formatTime. This creates a new DateTime object in php called $data that can also easily be manipulated if you need. The examples below formats the date in two different ways, both work.

Procedural Style:

function formatTime($millis) { 
        $date = new DateTime($millis); 
        return date_format($date, 'l, jS F Y \a\t g:ia'); 
}

Object Oriented Style:

function formatTime($millis) { 
        $date = new DateTime($millis); 
        return $date->format('l, jS F Y \a\t g:ia'); 
}

The format for date can be found at this link: http://php.net/manual/en/function.date.php

Cheers!

Upvotes: 0

cortexjesse
cortexjesse

Reputation: 171

If you have trouble with the data displaying from some of the other answers there is also this function strtotime which may help parse it.

echo date("F j, Y, g:i a", strtotime($timestamp));

Upvotes: 1

Kevin Cittadini
Kevin Cittadini

Reputation: 1469

Check here

PHP Date() Documentation

There's a table with every option

An example:

date('Y m d')

prints

2013 07 19

Upvotes: 1

Sumoanand
Sumoanand

Reputation: 8929

yes, you can use date function for that.

echo date("F j, Y, g:i a", $timestamp);

Output will be in following format:

// March 10, 2001, 5:16 pm

Upvotes: 4

user1646111
user1646111

Reputation:

you use this query:

SELECT DATE_FORMAT(timestamp, '%M %d, %Y %h:%i:%s %p') as mydate

see DATE_FORMAT for more info

Upvotes: 1

Related Questions