[RESOLVED] If today, check if after 1 PM EST
Here's my dilemma. my wifes flower shop has a delivery cut off at 1 PM EST. I need to check if the date is today... and if its after 1.
I can figure out how to check the time, because the person ordering could be in another time zone.
so, the delivery date is passed through post to a payment page as $delivery_date (php). Now, the user could start the order at noon, but wait an hour + before finishing and hitting submit on the payment page. So, how do i check the date ($delivery_date) and then, if its today, check if its after 1 PM EST. if so, dont submit and alert them.
I would assume JS would be best since i can do it on the form submit for validation....
i have code that stops the user from selecting today if after one, when they are at the delivery info page.
i THINK this is correct for taking into account the timezone...
Code:
var offset = -4.0;
var clientDate = new Date();
var utc = clientDate.getTime() + (clientDate.getTimezoneOffset() * 60000);
var d = new Date(utc + (3600000*offset));
if (dayOfWeek == 6 && d.getHours() > 12 || d.getHours() > 13) {
etc
etc
(note: on saturday, if after 12 EST then no good)
so how do i do it if the date value (ex: 7/30/2012) is stored in a POST or in a PHP variable.
thanks!
Re: If today, check if after 1 PM EST
This one is a good javascript library for date manipulation:http://code.google.com/p/datejs/
And if you want to use the date from a PHP variable, just echo it in the javascript.
Example:
PHP Code:
<?php
$msg = 'hi';
?>
<html>
<body>
<script>
var m = "<?php echo $msg; ?>";
alert(m);
</script>
</body>
</html>
:wave:
Re: If today, check if after 1 PM EST
EST is UTC-4, therefore 12pm/1pm EST is 4pm/5pm UTC, therefore:
Code:
var now = new Date();
if (now.getUTCHours() >= (now.getUTCDay() == 6 ? 16 : 17))
{
// stop
}
Of course, this isn't taking into account daylight savings.
You must validate also validate the order time on the server side.
Re: If today, check if after 1 PM EST
Hey Pen!
ok, can I safely assume DST is a pain?
how this look?
Code:
function validateDelTime()
{
var delDate = new Date(Date.parse("<?php echo $delivery_date ?>","MM/dd/yyyy"));
var dt = new Date();
var now = new Date();
// check if today
if (delDate.setHours(0,0,0,0) == dt.setHours(0,0,0,0))
{
// check if after 12/1 EST
if (now.getUTCHours() >= (now.getUTCDay() == 6 ? 16 : 17))
{
var ct = (now.getUTCDay() == 6 ? 12 : 1);
alert("The delivery cutoff time for today is " + ct + ":00 PM EST. Please go back select the next available day or call the store directly.");
return true;
}
return false;
}
}
@akhileshbc - thanks, but i dont want another whole file for a simple check.
Re: If today, check if after 1 PM EST
Why are you checking the date in JavaScript after it's already been posted to the server?
Shouldn't you check the delivery date before submitting the form?
Re: If today, check if after 1 PM EST
the first step is the user fills out the delivery information... address, names, date they would like it delivered. Then click submit. The Next step is payment..
If they start it before 1:00, but then wait till 1:30 or 2 or whatever, to finish and submit... I want to make sure that its not past 1:00 when they actually complete the order. I dont want an order to squeeze through when its too late. So, the date is not really posted.. its just passed from part one of checkout to part 2.. get it?
Re: If today, check if after 1 PM EST
OK, I get it... presumably then you want to do the validation at both stages (and on the server side as well).
Re: If today, check if after 1 PM EST
yes...
the calendar picker checks the time every time its opened, so you cant pick today if its after one.
then i want to do the same check (posted above) on both the delivery info page and the payment page.
so does that code look right? (I hate how it just breaks if you get it wrong lol)
Re: If today, check if after 1 PM EST
Looks fine. Don't you have a debugger?
Re: If today, check if after 1 PM EST
seems like this might be it...
i hit a snag with getUTCHours because once it hits 1 am it was less than 16 and the day became available again. so i THINK i have it right now
Code:
function validateDelTime()
{
var fld=document.forms["deliveryform"]["delivery_date"];
var delDate = new Date(Date.parse(fld.value,"MM/dd/yyyy"));//
var dt = new Date();
var now = new Date();
// check if today
if (delDate.setHours(0,0,0,0) == dt.setHours(0,0,0,0))
{
utc = now.getTime() + (now.getTimezoneOffset() * 60000);
nd = new Date(utc + (3600000*-4));
// check if after 12/1 EST
if (nd.getHours() >= (nd.getDay() == 6 ? 12 : 1))
{
var ct = (now.getDay() == 6 ? 12 : 1);
alert("The delivery cutoff time for today is " + ct + ":00 PM EST. Please go back select the next available day or call the store directly.");
return true;
}
return false;
}
}