Jump to content

Date and Time issue


dfowler

Recommended Posts

Hey guys, I created an online ordering system that has been working for awhile now.  However, it is starting to show some bugs.  When a user makes an order they get to option to choose when they want to pick it up.  Their options are 'Now', 'Today at:', and 'Tomorrow at:', here is the code for that part:

 

<script type="text/javascript">
function toggleTimeSelects() {
  orderforsel = document.getElementById('order_for');
  timesel = document.getElementById('time');
  minsel = document.getElementById('min');
  ampmsel = document.getElementById('ampm');
  if (orderforsel.value == "now") {
    timesel.disabled = true;
    minsel.disabled = true;
    ampmsel.disabled = true;
  } else if (orderforsel.value == '') {
    timesel.disabled = true;
    minsel.disabled = true;
    ampmsel.disabled = true; 
  } else {
    timesel.disabled = false;
    minsel.disabled = false;
    ampmsel.disabled = false;
  }
}
</script>
<tr>
    <td align="right">Pick-up Order:</td>
    <td>
<select name="order_for" id="order_for" onchange="javascript:toggleTimeSelects()">
	<option value="now">Now</option>
	<option value="today">Today at:</option>
	<option value="tomorrow">Tomorrow at:</option>
</select>
<select name="time" id="time" disabled>
	<?php for ( $counter = 1; $counter <= 12; $counter += 1) { ?>
	<option value="<?php echo $counter; ?>"><?php echo $counter; ?></option>
	<?php } ?>
</select>
<select name="min" id="min" disabled>
	<option value="00">00</option>
	<option value="15">15</option>
	<option value="30">30</option>
	<option value="45">45</option>
</select>
<select name="ampm" id="ampm" disabled>
	<option value="am">AM</option>
	<option value="pm">PM</option>
</select>
    </td>
  </tr>

 

The order is then saved in the database and the admin can view the orders in a special section of the website.  Here is the code that puts the time in the correct format:

$fix = mktime(date("H"),date("i")-10,date("s"),date("m"),date("d"),date("Y"));
$now1 = date("Y-m-d H:i:s", $fix);

if ($order_for == 'now') {
  $forDate = date('Y-m-d H:i:s');
} else if ($order_for == 'today') {
  $forDate = date('Y-m-d',time() + 60*60).' '.($ampm == 'am' ? str_pad($time,2,'0',STR_PAD_LEFT) : ($time + 12).':'.$min).':00';
} else {
  $forDate = date('Y-m-d',time() + 12*60*60).' '.($ampm == 'am' ? str_pad($time,2,'0',STR_PAD_LEFT) : ($time + 12).':'.$min).':00';
}

 

The problem is that when a user picks 'Today at: 12 30 PM' it saves in the database as: Dec 31,1969 7:00PM.  I'm not sure if other 'Today at:' times cause errors, but this one we know of for sure.  'Now' works and 'Tomorrow at:' works.  Can anybody see what might be causing this bug for orders made today?

Link to comment
https://forums.phpfreaks.com/topic/104213-date-and-time-issue/
Share on other sites

Not 100% sure what the issue is just by browsing the code but I know I get that date/time when I put an incorrect date time format into a datetime field.

 

Should be yyyy:mm:dd hh:ii:ss

 

If you're still getting that error see what you're actually echoing out. Maybe you're throwing in a space or something.

Link to comment
https://forums.phpfreaks.com/topic/104213-date-and-time-issue/#findComment-533511
Share on other sites

Well, since tomorrow works with a similar code I'm thinking it is something to do specifically with today.  I was going to mess around a little more and see if any of the other times give that error.  If I can pinpoint which times are doing it, then maybe I can find out why.  As I said before, the only time I know for sure causes an error is 'Today at: 12 30 PM'

Link to comment
https://forums.phpfreaks.com/topic/104213-date-and-time-issue/#findComment-533607
Share on other sites

Ok, I'm thinking of a way to completely redo this.  It seems I am still getting errors, and I just copied this code directly to test:

<?php

$order_for = today;
$time = 12;
$min = 30;
$ampm = pm;

if ($order_for == 'now') {
  $forDate = date('Y-m-d H:i:s');
} else if ($order_for == 'today') {
  $forDate = date('Y-m-d',time() + 60*60).' '.($ampm == 'am' ? str_pad($time,2,'0',STR_PAD_LEFT) : ($time + 12).':'.$min).':00';
} else {
  $forDate = date('Y-m-d',time() + 12*60*60).' '.($ampm == 'am' ? str_pad($time,2,'0',STR_PAD_LEFT) : ($time + 12).':'.$min).':00';
}


echo $forDate;
?>

 

Can anybody think of a better way to do this?  I'm going to keep plugging away, I'll gladly take suggestions though.  :)

Link to comment
https://forums.phpfreaks.com/topic/104213-date-and-time-issue/#findComment-533632
Share on other sites

I'm just going to keep talking on my own thread.  I figured out why it is causing a problem.  Didn't think of the 12.  Basically the code is saying if it is an 'am' just use the number.  If it is 'pm' add 12 (for military time).  This works great for something like 1 (am=1, pm=13).  However, with the number 12 this doesn't work.  So if a user picks 12:30pm, it is showing up at 24:30 (which isn't a valid time).

 

Now I have to figure out how to fix it.

Link to comment
https://forums.phpfreaks.com/topic/104213-date-and-time-issue/#findComment-533664
Share on other sites

This is my last post I promise!  Fixed the issue, just added another if statement.  Here is the new code:

 

if ($order_for == 'now') {
  $forDate = date('Y-m-d H:i:s');
} else if ($order_for == 'today') {
  $forDate = date('Y-m-d',time() + 60*60).' '.($ampm == 'am' ? ($time == 12 ? str_pad(($time - 12),2,'0',STR_PAD_LEFT).':'.$min.':00' : $time.':'.$min.':00') : ($time == 12 ? $time.':'.$min.':00' : ($time + 12).':'.$min.':00'));
} else {
  $forDate = date('Y-m-d',time() + 12*60*60).' '.($ampm == 'am' ? ($time == 12 ? str_pad(($time - 12),2,'0',STR_PAD_LEFT).':'.$min.':00' : $time.':'.$min.':00') : ($time == 12 ? $time.':'.$min.':00' : ($time + 12).':'.$min.':00'));
}

Link to comment
https://forums.phpfreaks.com/topic/104213-date-and-time-issue/#findComment-533694
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.