Jump to content

help with adding pm to a time selection


dadamssg

Recommended Posts

im trying to use mktime or gmmktime to calculate dates based on a date selection, month, day, year, hour, min, am/pm..and im trying to set it up where if pm is selected it will add 12 to the hours. it works fine from 1am-11pm...but when i input 12 pm it will calculate it as one hours short(11pm) but i tried to make some if stmts where if ampm equals 12 it will only add 11 hours but when i change that...it ouputs the next day 12am...really confused....

 

heres the code that outputs 11pm when 12pm is selected

if($_POST['eampm'] == "pm" && $_POST['ehour'] == "12")
   {
     $ehour = $_POST['ehour'] + 11;
        
     $_SESSION['ends'] = $_POST['eyear']."-".$_POST['emonth']."-".$_POST['eday']."- ".$ehour.":".$_POST['eminute'].":00";
      $end = mktime($ehour, $_POST['eminute'],0, $_POST['emonth'], $_POST['eday'], $_POST['eyear']);
   }
   elseif($_POST['eampm'] == "pm" && $_POST['ehour'] !== "12")
   {
   $ehour = $_POST['ehour'] + 12;
   $_SESSION['ends'] = $_POST['eyear']."-".$_POST['emonth']."-".$_POST['eday']."- ".$ehour.":".$_POST['eminute'].":00";
   $end = mktime($ehour, $_POST['eminute'],0, $_POST['emonth'], $_POST['eday'], $_POST['eyear']);
   }
   else{
   $end = mktime($_POST['hour'], $_POST['eminute'],0, $_POST['emonth'], $_POST['eday'], $_POST['eyear']);
   $_SESSION['ends'] = $_POST['eyear']."-".$_POST['emonth']."-".$_POST['eday']."- ".$_POST['hour'].":".$_POST['eminute'].":00";
   }

 

and heres the code that outputs march 31 12am if i input today, march 30 12pm

if($_POST['eampm'] == "pm" && $_POST['ehour'] == "12")
   {
     $ehour = $_POST['ehour'] + 12;
        
     $_SESSION['ends'] = $_POST['eyear']."-".$_POST['emonth']."-".$_POST['eday']."- ".$ehour.":".$_POST['eminute'].":00";
      $end = mktime($ehour, $_POST['eminute'],0, $_POST['emonth'], $_POST['eday'], $_POST['eyear']);
   }
   elseif($_POST['eampm'] == "pm" && $_POST['ehour'] !== "12")
   {
   $ehour = $_POST['ehour'] + 12;
   $_SESSION['ends'] = $_POST['eyear']."-".$_POST['emonth']."-".$_POST['eday']."- ".$ehour.":".$_POST['eminute'].":00";
   $end = mktime($ehour, $_POST['eminute'],0, $_POST['emonth'], $_POST['eday'], $_POST['eyear']);
   }
   else{
   $end = mktime($_POST['hour'], $_POST['eminute'],0, $_POST['emonth'], $_POST['eday'], $_POST['eyear']);
   $_SESSION['ends'] = $_POST['eyear']."-".$_POST['emonth']."-".$_POST['eday']."- ".$_POST['hour'].":".$_POST['eminute'].":00";
   }

Link to comment
https://forums.phpfreaks.com/topic/151788-help-with-adding-pm-to-a-time-selection/
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.