简体   繁体   中英

Day of the week field

I have a php database that shows a listing of events...date, end_date event_name and such.

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.

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. 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. There is this handy PHP function, called date() that takes a 'timestamp' and converts it into readable text, including the day of the week. See:

http://www.php.net/manual/en/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.

Now you need the 'timestamp'. I normally use the strtotime() function for that:

http://nl3.php.net/manual/en/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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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