This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How to compare the date parts of two Zend_Date objects?
I've been trying to compare dates, but for some reason things don't seem to work as I want them to.
I'm trying to log to a database whenever someone logs into my site on a certain day.
I've used if (date("j F") == "25 July") followed by my code, but it doesn't seem to trigger.
I intend to get it to check for a lot of things, such as Friday 13, 17 March, etc (obviously I know I'll need to change the date() format for Friday 13).
The current check of 25 July is a copy from an echo of date(), so I know it's definitely not a typo. Can anyone tell me why this isn't working, and how to do it?
Cheers
The best method is to use a strtotime function. But I think it won't work until you specify year in the date.
Not sure about it though. But strtotime is a good option.
Related
This question already has an answer here:
PHP date() shows tomorrow's date
(1 answer)
Closed 8 years ago.
I have a basic php script
<?php
$today = date("Y-m-d");
echo $today;
?>
Which should output 2014-11-14. However, I am getting output of 2014-11-15 even though my system tray displays 2014-11-14.
I changed the system date back one day (13th) and I got the output I wanted (14th)... Earlier today I had to do a system restore and ran Malwarebytes because I picked up a virus. Could this be the cause of this?
Does anyone know where else I can check my systems time other than the system tray?
date() function uses unix timestamps which always is set to +0:00
So use:
date_default_timezone_set('Europe/Zurich');
When you set your default timezone it will automaticly calculate the offset!
the date() function is returning UTC time and not the time on your local computer
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.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Convert one date format into another in PHP
I am currently pulling from my SQL database the event dates i.e in the table field date it will display (2011-12-08) however i wish to have it output December 8th, 2011 rather than 2011-12-08 when display through php on my website. Any ideas on how to go about this?
As people have said, for questions like this please search around first before asking since many places on the web and many other questions in stackoverflow already cover this. That said, my advice is to store the date in the database as a datetype because it will allow mysql to do its operations easier then convert the the human readable form upon displaying it using the php date function which would use format "F jS, Y" for what you want. Good luck and welcome to stackoverflow.
Quick embrassing question.
I have been looking for a PHP function that would calculate the difference between two timestamps and output the result based on given parameters such as
the diff in years only, diff in months only, diff in days only, etc etc
The function I made has been quite buggy and I haven't found a good one on the Net.
Please assist.
Thanks
Please take a look at DateTime::diff()
DateTime::diff — Returns the difference between two DateTime objects
You can format the output to anything you want it to be
(Provided an extra answer despite of the duplicates because they use strtotime & math and that doesn't always work out well or is a nice way to do it. Using a core function of php seems nicer to me)
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