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
Solved

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

Posted on 2013-12-06
10
267 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 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
}
else
{
    // deny changes
}

Open in new window

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 
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 109

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
 
LVL 109

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 109

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 109

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Google Maps API and PHP 25 49
How do uses indexes to maximize MySQL Searches 14 55
How is this connection happening? 3 20
php help 34 58
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

809 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