问题
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