Get date for past weekday with Smarty

浪尽此生 提交于 2019-12-13 09:49:21

问题


I'm still fairly inexperienced with scripting, so my knowledge is limited to working with smarty.

I'm working on building a daily news site, and we are going to have an archive of news articles from every day from the past week. If the day of the week hasn't yet passed, it will show the articles from that day of the week last week. On each day's page, I want a head line that says "News from Monday, June 10th" or whatever the date for the last one of those days of the week was.

As far as I can tell, the $smarty.now function is all relative to today's date, so I can't use that. If there is any way I can do this with smarty, or if someone knows of a PHP script that I can include, that would be greatly appreciated.


回答1:


This is logic that you would normally put inside your PHP script, not smarty. Try something like this:

empty($_GET['day']) ? $day = date('l') : $day = $_GET['day'];
$date = strtotime("last {$day}");
echo "News from ".date("l, F j", $date);

This script assumes that you pass the day as a GET parameter, for example index.php?day=monday. You can assign what's echo'ed to smarty. strtotime() and date() are both powerful and flexible functions, have a look at their PHP documentation



来源:https://stackoverflow.com/questions/17112491/get-date-for-past-weekday-with-smarty

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!