Solved

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

Posted on 2013-12-06
10
260 Views
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.

Correct?
0
Comment
Question by:brucegust
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 18

Assisted Solution

by:Matthew Kelly
Matthew Kelly earned 167 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.
0
 

Author Comment

by:brucegust
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?
0
 
LVL 74

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
}
else
{
    // deny changes
}

Open in new window

0
 
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39701254
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

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.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39701290
See if this helps sort it out...
http://www.laprbass.com/RAY_temp_brucegust.php

<?php // RAY_temp_brucegust.php
error_reporting(E_ALL);
echo '<pre>';

$now = date('c');
$inp = !empty($_GET['t']) ? strtotime($_GET['t']) : FALSE;
if ($inp)
{
    $inp = date('c', $inp);
    if ($inp <  $now) echo PHP_EOL . "INPUT DATETIME IS BEFORE CURRENT DATETIME";
    if ($inp >  $now) echo PHP_EOL . "INPUT DATETIME IS AFTER CURRENT DATETIME";
    if ($inp == $now) echo PHP_EOL . "INPUT DATETIME IS EXACTLY CURRENT DATETIME";
}

echo PHP_EOL . "CURRRENT DATETIME IS $now";

$form = <<<EOD
<form>
ENTER A DATE AND TIME TO COMPARE TO THE CURRENT DATE AND TIME
<input name="t" /><input type="submit" value="Go!" />
</form>
EOD;

echo $form;

Open in new window

HTH, ~Ray
0
 

Author Comment

by:brucegust
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?
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 333 total points
ID: 39701636
coding glory
Ha!

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?
0
 

Author Comment

by:brucegust
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:

$today=date("Y-m-d");
$marketing_period_end=2013-12-04;

if($today<=$marketing_period_end)
{
//user can do stuff
}
else
{
//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?
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 333 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.
$marketing_period_end=2013-12-04;
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now