php 将日期转换为日期名称,例如 Mon、Tue、Wed

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15388578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:08:15  来源:igfitidea点击:

Convert date to day name e.g. Mon, Tue, Wed

php

提问by user1444027

I have a variable that outputs the date in the following format:

我有一个以以下格式输出日期的变量:

201308131830

Which is 2013 - Aug - 13 - 18:30

这是 2013 年 - 八月 - 13 - 18:30

I am using this to retrieve the day name but it is getting the wrong days:

我正在使用它来检索日期名称,但它的日期错误:

$dayname = date('D', strtotime($longdate));

Any idea what I am doing wrong?

知道我做错了什么吗?

回答by John Conde

This is what happens when you store your dates and times in a non-standard format. Working with them become problematic.

当您以非标准格式存储日期和时间时,就会发生这种情况。与他们合作成为问题。

$datetime = DateTime::createFromFormat('YmdHi', '201308131830');
echo $datetime->format('D');

See it in action

看到它在行动

回答by Albert Naa

Your code works for me

你的代码对我有用

$date = '15-12-2016';
$nameOfDay = date('D', strtotime($date));
echo $nameOfDay;

Use l instead of D, if you prefer the full textual representation of the name

如果您更喜欢名称的全文表示,请使用 l 而不是 D

回答by Rayees Pk

echo date('D', strtotime($date));
echo date('l', strtotime($date));

Result

结果

Tue
Tuesday

回答by Sammitch

Your code works for me.

你的代码对我有用。

$input = 201308131830; 
echo date("Y-M-d H:i:s",strtotime($input)) . "\n";
echo date("D", strtotime($input)) . "\n";

Output:

输出:

2013-Aug-13 18:30:00
Tue

Howeverif you pass 201308131830as a numberit is 50 to 100x larger than can be represented by a 32-bit integer. [dependent on your system's specific implementation] If your server/PHP version does not support 64-bit integers then the number will overflow and probably end up being output as a negative number and date()will default to Jan 1, 1970 00:00:00 GMT.

但是,如果您201308131830作为数字传递,它会比 32 位整数表示的大 50 到 100 倍。[取决于您系统的具体实现] 如果您的服务器/PHP 版本不支持 64 位整数,那么该数字将溢出并可能最终被输出为负数并date()默认为Jan 1, 1970 00:00:00 GMT.

Make sure whatever source you are retrieving this data from returns that date as a string, and keep it as a string.

确保您从中检索此数据的任何来源将该日期作为字符串返回,并将其保留为字符串。

回答by plaidcorp

Seems like you could use date() and the lowercase "L" format character in the following way:

似乎您可以通过以下方式使用 date() 和小写“L”格式字符:

$weekday_name = date("l", $timestamp);

Works well for me, here is the doc: http://php.net/manual/en/function.date.php

对我来说效果很好,这里是文档:http: //php.net/manual/en/function.date.php

回答by Alvaro

You can not use strtotimeas your time format is not within the supported date and time formats of PHP.

您不能使用,strtotime因为您的时间格式不在PHP 支持的日期和时间格式范围内

Therefor, you have to create a valid date format first making use of createFromFormat function.

因此,您必须首先使用createFromFormat 函数创建有效的日期格式。

//creating a valid date format
$newDate = DateTime::createFromFormat('YmdHi', $longdate);

//formating the date as we want
$finalDate = $newDate->format('D'); 

回答by ARN

For other language use day of week to recognize day name

对于其他语言,请使用星期几来识别日期名称

For example for Persian use below code

例如波斯语使用下面的代码

$dayName = getDayName(date('w', strtotime('2019-11-14')));

    function getDayName($dayOfWeek) {

        switch ($dayOfWeek){
            case 6:
                return '????';
            case 0:
                return '?? ????';
            case 1:
                return '?? ????';
            case 2:
                return '?? ????';
            case 3:
                return '???? ????';
            case 4:
                return '??? ????';
            case 5:
                return '????';
            default:
                return '';
        }

    }

More info : https://www.php.net/manual/en/function.date.php

更多信息:https: //www.php.net/manual/en/function.date.php