For beginners: Programming is not all about chars but about logic

Started on the old forum in May 2011 · view the original in the archive · old links redirect here

No replies · 315 views · started 15 years ago · 1 min read

Mysterio Lead · 630 posts #1

Today, I was asked to make a query which shows only the posts that has been posted yesterday or earlier. This was because a graduated guy was "unable to find a function" to do this (!).

Here is a small script how it can be done using easy functions and logic:

  1. Find the exact day, month and year of today.

$day = date("d", time()); // date('d'); will also work but I am using the second parameter if needs to change timezone or I don't know $month = date("m", time()); $year = date("Y", time());

  1. Knowing that the day begins at 00:00 (midnight), we need to convert this hour into time() format, so we can deal later with it.

$today = mktime("00", "00", "00", "$month", "$day", "$year");

The above function, gives to the variable $today the exact value in seconds which means the time: 00hours, 00minutes, 00seconds, $day days, $month months, $year years. The $day, $month and $year were filled in the step 1.

  1. Now, to complete our mission, the logic is like this:

Select something from somewhere, where the entry date is lower as a numeric value, than today. Remember that today started 00:00. Even one second before this time, it makes it "yesterday".

So here is the final query:

SELECT something FROM somewhere WHERE entry_date < '$today'


In my case, the code was: $today = mktime("00", "00", "00", date('m'), date('d'), date('y'));

mysterio.al - programming is a functional art

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close