简体   繁体   English

星期几字段

[英]Day of the week field

I have a php database that shows a listing of events...date, end_date event_name and such. 我有一个php数据库,该数据库显示事件列表... date,end_date event_name等。

It shows like this on the website: Date End Date Event Time Event Name etc... 它在网站上显示如下:日期结束日期事件时间事件名称等...

My client wants to add the day of the week, next to the date field. 我的客户想要在日期字段旁边添加星期几。 I can't alter the date field (which is showing as: Month Day). 我无法更改日期字段(显示为:月日)。 So if the date is August 29 (which is tomorrow), he wants it to display as: Friday, August 29. 因此,如果日期是8月29日(即明天),则他希望将其显示为:8月29日,星期五。

Is there a way to go about this, easily? 有没有办法轻松做到这一点? I am not a seasoned programmer. 我不是一个经验丰富的程序员。

Thanks 谢谢

As noted by 'sjagr' the day of the week depends on the month, day of month, and year. 正如“ sjagr”所指出的那样,星期几取决于月份,月份和年份。 Since the year information is missing in your database, and you cannot alter it, you cannot show the day of the week. 由于数据库中缺少年份信息,因此您无法更改它,因此无法显示星期几。

Ok, suppose you do know the year. 好吧,假设您知道年份。 What then? 然后怎样呢? Todd Withers gave the correct hint. 托德·威瑟斯给出了正确的提示。 This is not as difficult as it may seem, because PHP, or rather your OS, knows a lot about dates. 这并不像看起来那样困难,因为PHP或您的OS对日期了解很多。 There is this handy PHP function, called date() that takes a 'timestamp' and converts it into readable text, including the day of the week. 有一个方便的PHP函数,称为date(),它带有一个“时间戳”,并将其转换为可读的文本,包括星期几。 See: 看到:

http://www.php.net/manual/en/function.date.php http://www.php.net/manual/zh/function.date.php

A good starting format would be 'l, F j' for the USA, but if you live somewhere else you should change it to something a bit more normal. 对于美国来说,一个好的开始格式是“ l,F j”,但是如果您住在其他地方,则应将其更改为更普通的格式。

Now you need the 'timestamp'. 现在您需要“时间戳记”。 I normally use the strtotime() function for that: 我通常为此使用strtotime()函数:

http://nl3.php.net/manual/en/function.strtotime.php http://nl3.php.net/manual/zh/function.strtotime.php

It's quite flexible and can handle quite a few ways of writing a date. 它非常灵活,可以处理多种写日期的方法。 A complete example in code would be: 代码中的完整示例为:

$year  = '2014';  // I use strings, but numbers can also work
$month = '08';    // this is numeric, but names can also work  
$day   = '29';

$timestamp = strtotime("$year-$month-$day");

echo date('l, F j',$timestamp);

Experiment with it, try different formats! 尝试一下,尝试不同的格式!

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

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