简体   繁体   English

如何在PHP上按日期获取星期几?

[英]How to get day of week by date on PHP?

I get date (ex. 2015-06-12) and i want to get day of week... 我知道日期(例如2015-06-12),并且想知道星期几...

For example Monday = 1 例如星期一= 1

$dateprog=$_GET['dateprog'];
$dow = date("N",$dateprog);

but it's not working 但它不起作用

The variable $dateprog is in string format which you are extracting from the 变量$ dateprog是从中提取的字符串格式。

$_GET['dateprog'] $ _GET ['dateprog']

and parsing it as a parameter to the date function. 并将其解析为日期函数的参数。 However, the date function takes timestamp or date format as its second parameter 但是,date函数将时间戳或日期格式作为其第二个参数

string date ( string $format [, int $timestamp = time() ] ) 字符串日期(字符串$ format [,int $ timestamp = time()])

See documentation : Date 查看文件: 日期

In order to convert the string to timestamp use the strtotime function as follows: 为了将字符串转换为时间戳,请使用strtotime函数,如下所示:

if(isset($_GET['dateprog']))
{
    $dateprog = $_GET['dateprog'];
    $dow = date("N",strtotime($dateprog));
    echo $dow;
}

Try with - 尝试-

if (!empty($_GET['dateprog'])) {
    $dateprog=$_GET['dateprog'];
    $dow = date("N",strtotime($dateprog));
    echo $dow; // will print the day number of the week
}

just cast it to time: 只需将其转换为时间即可:

$dateprog=$_GET['dateprog'];
$dow = date("N",strtotime($dateprog));

用这个

$dow = date("N",strtotime($dateprog));

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

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