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.
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
Related
I am trying to add weekdays to a date using the below formula:
$date = strtotime($effdate." +5 weekdays");
$date = date('m/d/Y', $date);
It works fine for other days but not for Friday. It points to the next Sunday rather than Friday. I googled for many solutions, but didn't get a clear idea.
Is there a workaround to fix this bug?
If you're running PHP < 5.5.0 then this is a known bug and was fixed in PHP 5.5
Demo
The bug report does provide an alternative function that will work with versions of PHP that are susceptible to this bug, though it's using DateTime objects rather than unix timestamps
Well,this seems strange. Please bear with me. Someone asked this question in SO. He wants the date of previous Monday. So i suggested
$monday=date(Y-m-d,strtotime('Monday this week'))
The output was perfect in my localhost. It showed 2012-07-30. Another guy commented that the function i mentioned isn't working. It is giving the same date like
$monday=date(Y-m-d,strtotime('Monday'))
i.e., 2012-08-06. And he isn't lying! The online editor which he linked is showing next monday's date. Check this! Why is this happening??
I searched, but couldn't get the reason behind it. Is it because of the older versions of php? Any help will be greatly appreciated. Thank you
Somewhere between 5.2.17 and 5.3.10 the problem was fixed: http://viper-7.com/1PPz5m (look at the paste history).
Digging around in the changelog for the 5.3.0 release I found this:
proper support for "this week", "previous week"/"last week" and "next week" phrases so that they actually mean the week and not a seven day period around the current day.
Sounds like that's probably the answer to me. Basically before 5.3.0 this week etc may give you the wrong answer, because it will look for the day in the 7 days surrounding the current date that is a Monday, whereas in 5.3.0 and later it will be interpreted correctly.
Hello I have a question about relative time formats in PHP. I am looking to get the time "month to date." For example, Today is May 5th, I would like to get the time span from May 1st, to May 5th. I tried using the format "this month," but I did not have success. Can anyone point me in the right direction?
Will strtotime() work for you? It takes a string such as next month and then creates a timestamp of it.
If not, take a look at this Stack Overflow question.
Another option would be using DateTime which is builtin to PHP as well.
I'm pulling the most recent listened tracks from last.fm and putting them on my website.
Problem is, the times are retrieved in UTC-0 uts format and appear to be an hour out when comparing them to BST times in order to calculate a fuzzy time stamp ("about 5mins ago", "about an hour ago" etc).
Is there any way solve this so the times always match BST/GMT and adjust when entering and leaving daylight saving time?
Here's a snippet of PHP code i'm using at the moment, which results in the times being an hour out.
$now = time(); // use this so all times are to the same second
$tz = getenv("TZ"); // save local setting so we can reset it later
putenv("TZ=Europe/London");
$trackPlayedAt = date('d M Y H:i:s', $track->date->uts);
date() automatically formats to the local timezone. The timezone depends on the configuration of the PHP server. If everything is set correctly, it should just work.
If you are running PHP 5.3 you have more options. Comment with which version of PHP you are running.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I calculate relative time?
I want to format dates on my social web app much like Digg.com and other sites do. There, you see very friendly dates, such as:
just now
3 minutes ago
one hour ago
2 weeks ago
6 months ago
etc
Before I wrap my head around creating such a thing, does anyone know of any ready-to-go script for this where I simply insert a datestamp and a friendly date text is given based on how it related to the current time?
PS: I need this in PHP, but pseudo-code or any other language is fine too.
This is a duplicate of this question. It has a flurry of code samples on how to accomplish this, in addition to the code this very site uses. I glanced at it and there seems to be a PHP implementation posted there too.
In addition to all this, if are you using jQuery you can do this client-side with something like the timeago plugin. It has the advantage of updating the text as time passes so if you load a page and it says "posted 5 minutes ago" and look again 5 minutes later, it says "posted 10 minutes ago"
Thanks all for the answers, and sorry for the duplicate question. I did not find the duplicate when I was looking for it because I did not really know what search terms to use.
Anyways, I have my problem solved thanks to the PHP translation of the code used by stackoverflow. I made one tiny change in calculating the delta:
$delta = strtotime(gmdate("Y-m-d H:i:s", time())) - $time;
Since I am storing my dates in MySQL as timestamp in the GMT format, I have to use the same for calculating the CURRENT time. This makes for a timezone neutral comparison, which is exactly what is needed in my case.
You can also do this in SQL:
Best way to convert DateTime to "n Hours Ago" in SQL