与Smarty取得过去平日的日期 [英] Get date for past weekday with Smarty

查看:111
本文介绍了与Smarty取得过去平日的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然没有脚本经验,所以我的知识仅限于与smarty合作。

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

我正在建设一个每日新闻网站,我们将从过去一周的每一天收录新闻文章。如果星期几还没有通过,那么它将会显示上星期的那一天的文章。在每一天的页面上,我想要一个头条线,表示新闻从6月10日星期一或任何一周的最后一天的日期。

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.

据我所知,$ smarty.now函数都是相对于今天的日期,所以我不能使用。如果有任何方式,我可以用smarty做这个,或者如果有人知道我可以包括的PHP脚本,那将是非常感激的。

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.

推荐答案

这是你通常放在PHP脚本中的逻辑,而不是smarty。尝试这样的东西:

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);

此脚本假定您将日期作为GET参数传递,例如index.php?day =星期一。你可以指定什么是echoyed为smarty。 strtotime() date()都是强大而灵活的功能,看看他们的PHP文档

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

这篇关于与Smarty取得过去平日的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆