MKO
MKO

Reputation: 41

Time and date echo from mysql table

I have this question:

I have selected date & time from the table & echoed them just fine but I don't like the date & time format that is echoed. Here in Central-Southern Africa, we are used to 24hrs (like 16:30hrs instead of 4:30pm etc).

Here is the code:

("SELECT * FROM me order by 1 DESC LIMIT 0,10");
$date = $run_post['date'];

$time = $run_post['time'];

And them I do this:

echo $date; 

and it gives me 2015-11-13 but I want 13th November 2015 and then

echo $time; 

giving me 2015-11-13 12:53:43 but want like 16:30:00 hrs format.

Finally, I also want to echo my (UTC+02:00) Cairo Time zone. Currently it is giving me -2hrs

Upvotes: 1

Views: 722

Answers (3)

Rajdeep Paul
Rajdeep Paul

Reputation: 16963

I'm assuming that your default time zone is set to UTC. If you want to display time as per your time zone(UTC+02:00), then you can do something like this:

function display_time($time){
    $unixdatetime = strtotime($time) + 7200;
    return strftime("%d %B %Y, %H:%M %p",$unixdatetime);
}

And when you call this display_time function, for example,

echo display_time($run_post['time']);

then it would display,

13 November 2015, 14:53 PM

Upvotes: 0

Dhouard
Dhouard

Reputation: 175

format method of DateTime class is what you need.

$date = new DateTime($run_post['time']);
echo $date->format('d\t\h\,F Y');

Will echo something like 13th, November 2015)

You have the documentation of how to format in

https://secure.php.net/manual/en/function.date.php

Upvotes: 0

KiwiJuicer
KiwiJuicer

Reputation: 1972

You should use the DateTime Class as you can set the timezone in it.

Example with Timezone Europe/London:

$date = new DateTime('2015-11-13 12:53:43', new DateTimeZone('Europe/London'));
echo $date->format('d\t\h F Y') . "\n";
echo $date->format('H:i:s') . 'hrs';

How to add Timezone to DateTime

Upvotes: 1

Related Questions