简体   繁体   English

从 mysql 表回显时间和日期

[英]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).在中南部非洲,我们习惯于 24 小时制(例如 16:30 小时而不是下午 4:30 等)。

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它给了我2015-11-13但我想要2015 年 11 月 13 日然后

echo $time; 

giving me 2015-11-13 12:53:43 but want like 16:30:00 hrs format.给我2015-11-13 12:53:43但想要像16:30:00 小时格式。

Finally, I also want to echo my (UTC+02:00) Cairo Time zone.最后,我还想回应我的 (UTC+02:00) 开罗时区。 Currently it is giving me -2hrs目前它给了我 -2hrs

You should use the DateTime Class as you can set the timezone in it.您应该使用 DateTime 类,因为您可以在其中设置时区。

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如何将时区添加到日期时间

format method of DateTime class is what you need. DateTime 类的 format 方法正是您所需要的。

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

Will echo something like 13th, November 2015)将回声类似于 2015 年 11 月 13 日)

You have the documentation of how to format in您有关于如何格式化的文档

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

I'm assuming that your default time zone is set to UTC.我假设您的默认时区设置为 UTC。 If you want to display time as per your time zone(UTC+02:00), then you can do something like this:如果您想按照您的时区(UTC+02:00)显示时间,那么您可以执行以下操作:

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,例如,当您调用此display_time函数时,

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

then it would display,然后它会显示,

13 November 2015, 14:53 PM

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM