Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Can someone check my logic just so I know for sure that I'm doing this right?

Posted on 2013-12-06
Medium Priority
Last Modified: 2013-12-11
A student goes out to my page to make some last minute changes knowing that the cutoff is drawing near. In this case, it's December 4th. She has to have all of her updates done before the clock strikes 12:00 AM, December 5th.

The code that I have written which defines that dynamic is this:

if($marketing_end_four<=$today AND $mk_four_close=="0000-00-00")
//empty the system of all entries that have been made up to midnite the day the marketing period ends.

$marketing_end_four in this case is 2013-12-04. My "<-," in my mind, means that all the entries that have been made up to 11:59 PM on the 4th will be processed.

Before I had the code set up so it read "if($marketing_period_four<$today...so what was happening is that you couldn't make any changes on the day the marketing period ended. But with the "<=," now I've got things where the user can change things right up to 12:00 AM on the fifth.

Question by:brucegust
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
LVL 18

Assisted Solution

by:Matthew Kelly
Matthew Kelly earned 668 total points
ID: 39701057
Just to make sure... both $marketing_end_four and $today are date variables and not strings correct?

If $today is set to something like $today = time(); it will have both date and current hours in it while the end_four variable which was just set to 2013-12-04 will really be "2013-12-04 00:00:00.000" because you didn't pass any hours/minutes/seconds to it.

So you need to add the hours/minutes to marketing_end_four i.e make it "2013-12-04 23:59:59.999" or simply do

The less than or equal still won't work because you will be comparing "2013-12-04 00:00:00.000" <= "2013-12-04 09:54:35.000" which will still be false.

Author Comment

ID: 39701093
Alright, Matt! First off all, I appreciate your time so thanks for getting back with me.

I've got $today set to $today=date("Y-m-d") so there's no "timeframe" involved at all, I'm just looking at, from what you're suggesting: if(2013-12-04 <= 2013-12-04)...

So, maybe that's not enough to accommodate the fact that these folks need up to midnite to make their changes, yes?

If that's the case, what do I need to do?

From what I can gather, based on what you've said up top, is that even with the hours and seconds added, I'm still going to come up short.

So what are my options? How do I need to accommodate the "the bell strikes at midnight" thing?
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39701127
Why not set a variable to hold December 5th, at midnight and then compare that the current date is less than (not less than or equal) that variable?

Pseudo-Code (I don't fully understand PHP's date/time structures, so this code is just for example)
$cutoff = "2013-12-04 00:00:00";
$now = get_date();

if ($now < $cutoff)
    // user can still do stuff
    // deny changes

Open in new window

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

LVL 18

Expert Comment

by:Matthew Kelly
ID: 39701195
Bruce, if neither the $today nor $end_marketing_four have time (both 00:00:00.000) then less then or equal will work. You can confirm by outputting both vales and ensuring houtd minutes seconds are all zeros.

For future code maintainability though you will want to set the end to 23 59 59 999 in case the $today value is changed to date and time.
LVL 111

Expert Comment

by:Ray Paseur
ID: 39701254

PHP time() does not include milliseconds.  PHP's interpretation of "now" includes time.  Its interpretation of "today" does not include time, and implies a time of 00:00:00.

The timezone, either explicitly set or implied by a configuration setting, is in play when you try to establish a cutoff.  If the cutoff is midnight, this begs the question, "Midnight where?" since midnight in my time zone (Chicago) happens two hours before midnight in Seattle.  However the value of time() will be the same worldwide.

The MySQL data base engine can have a different timezone setting from the PHP engine; they are completely independent and your programming must account for this.

I'll try to give you a code sample you can test with.
LVL 111

Expert Comment

by:Ray Paseur
ID: 39701290
See if this helps sort it out...

<?php // RAY_temp_brucegust.php
echo '<pre>';

$now = date('c');
$inp = !empty($_GET['t']) ? strtotime($_GET['t']) : FALSE;
if ($inp)
    $inp = date('c', $inp);


$form = <<<EOD
<input name="t" /><input type="submit" value="Go!" />

echo $form;

Open in new window

HTH, ~Ray

Author Comment

ID: 39701430
Ray, as always, I bow before your coding glory...!

Here's my question: When I built this thing, I didn't factor in time at all, just date. So now that there's a timeframe needing to be considered, what I'm hearing you say resonates in a couple of ways.

First of all, I need to establish a benchmark. If someone the West Coast goes to insert a value, thinking they've got an hour before the gates close, if my server is sitting in Tennessee, they're going to be in for a surprise. So I've got to establish the timezone on my server and ensure that's reflected throughout the app so no one is caught off guard.

As far as my initial dilemma, provided I establish the date on my app so it's founded on the Central Time Zone, is my code sufficient to ensure that if the date that they're entering their info (which is simply 2013-12-04 as opposed to a detailed timestamp) is going to accomplish what I'm trying to accommodate as far as keeping the gates open till 12:00 AM on the fifth?

In other words, what assumptions are made by PHP when it sees 2013-12-04? Does it automatically assume 12:00 AM or is there an assumed timeframe that could render my code pointless should they attempt to make any changes the day that the marketing period closes?

I should also add that when one of my users adds info, the date of that interaction is recorded as 2013-12-04 and not as a timestamp, so there's no specific "times" involved at all with the way I've coded things.

So, having said all that, do I need to just add a date_default_timezone_set("America/Chicago"); somewhere and I'll be OK, or what do I need to do based on what I currently have in place to make sure that if the marketing period closes on the fourth, the system doesn't do anything before 12:00 AM on the fifth?
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1332 total points
ID: 39701636
coding glory

As I see it there are a few moving parts to the question.  Let's take the human interface first.  Anything you do should be done with transparency, so I would have a prominent statement on the web site that says "Marketing Period Closes at Midnight Central Time on December 4th"  The statement might be accompanied with a color indicator like a traffic light showing green, then yellow as the deadline approaches, then red ("Marketing Period Has Closed") when the date changes from the 4th to the 5th and the period is closed.

The machine parts are simpler.  If the only information you're tracking is the date, and the period ends at midnight on your server's time, you can simply ignore the time of day.  You want to use date_default_timezone_set() or set the timezone in your configuration since it will be part of the implied contract with your clients.

I expect you do not need to think about MySQL's time zone.  You can handle everything in PHP.

Does that make sense for your requirements?

Author Comment

ID: 39701694
I believe so. Let me explain things back to you just so I can sound intelligent when I speak to my client.

The code that I have in place is fundamentally sufficient as far as the allowing for a person to be making edits and adjustments up to midnite on the fourth given the current configuration of my code:


//user can do stuff
//doors are closed

Ideally, best practices would dictate that I code this in the context of a timestamp. But, while I didn't necessarily plan it this way, what I have works because PHP is going to assume that a plain date is going to include the timeframe between 12:00 AM and 11:59 PM.

The reason some of my users might've run into an issue is that if they're making edits at 11:05 PM on the West Coast, if my server is sitting on the East Coast, the time according to the system is 12:05 AM and that is where my problem is.

By establishing the timeframe on my server, regardless of where the service itself resides, I can maintain the consistency I need for all my users regardless of where they're at.

Is that accurate?
LVL 111

Accepted Solution

Ray Paseur earned 1332 total points
ID: 39701823
Philosophically accurate, yes.  

Timezones are standardized and named, and the difference between US East Coast ("America/New_York") and US West Coast ("America/Los_Angeles") is three hours.

This statement will create an integer value of 1997.  You probably want a quoted literal string.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question