Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



PHP – Determine if ...
 
Share:
Notifications
Clear all

[Solved] PHP – Determine if this is the same day …

1 Posts
1 Users
0 Reactions
1,268 Views
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter  

We would like to see if TODAY is the same day that post has been posted.
To get TODAY:

    date('Y-m-d')

This will return something like '2010-04-09'. The problem is that we have our post date as a string, for example: 

    $postingdate='2010-04-09 12:34:22'

So is this the same date?

Option 1:

if (date('Y-m-d')!=date('Y-m-d',strtotime($postingdate))) { echo 'Not the same day'; };

This converts the string to a timestamp, and then back to a Y-m-d format.

Option 2:

if (strpos($postingdate,date('Y-m-d'))===FALSE) { echo 'Not the same day'; };

Does try to find the date ('2010-04-09') in the string '2010-04-09 12:34:22'.
If it does: same day!

The latter seems more efficient, as the strtotime function might need more "time" to determine the string format that it's trying to convert to a timestamp, to then convert it again to a string.


   
ReplyQuote
Share: