Jump to content

Time/Date formatting


CodeMama

Recommended Posts

I have a form that collects a user selected date into three fields:

 

$startmonth

$startday

$startyear

 

yet above the form field I have a note telling users that they can not enter a date before a certain one using this little snippet:

   <?php 
                       $adstart  = date("d-M-Y", mktime( 0,0,0, date("m") , date("d")+2, date("Y")));
                       echo  ("$adstart"); 
                        ?>

 

This is a intranet app and the users still can't be trusted to play by the rules so I have to enter that date for them now, my question then is how can I break up that function there to enter it in the db in the month, day , year parts...?

 

Link to comment
Share on other sites

Well almost, it would work but the date when broke down to the variables

$StartMonth

$StartDay etc are in the Unix box time

 

I tried changing it to :

    $adstartstamp  = date("d-M-Y", mktime( 0,0,0, date("m") , date("d")+2, date("Y")));
                        $StartMonth = date("m",$adstartstamp);
                        $StartDay = date("d",$adstartstamp);
                        $StartYear = date("Y",$adstartstamp);  

 

and the complete $adstartstamp echos the readable date but the other breakdown parts give the Unix time.  How do I fix that then I'm good to go.

Thanks

Link to comment
Share on other sites

date() returns a formatted date. date()'s second argument needs to be unix timestamp.

 

i'm not sure exactly what you are asking, but try this:

$adstartstamp  = mktime( 0,0,0, date("m") , date("d")+2, date("Y"));
$adstart  = date("d-M-Y", $adstartstamp);
$startmonth = date("m",$adstartstamp);
$startday = date("d",$adstartstamp);
$startyear = date("Y",$adstartstamp);

 

Link to comment
Share on other sites

I don't want the unix date format

I need to put in the regular format like : February etc

 

although yay for bosses, she just changed the task...oi.

Now I just have to keep the user from entering a date before what is allowed..this is a whole new can of worms.

Link to comment
Share on other sites

well, let's start with the form...you probably want to build <SELECT> statements for the Month/Day/Year. Make the display value whatever you want, but have the option value be the numeric representation (1 for Jan, 2 for Feb, etc) you can also google for a JavaScript datepicker, there are tons out there.

 

next in your PHP, let's validate. so in the script the form is posted to, it would be something like this:

$submitted = mktime( 0,0,0, $_POST['month'] , $_POST['day'] , $_POST['year'] ); //date the user submitted as a timestamp
$min = mktime( 0,0,0, date("m") , date("d")+2, date("Y")); //2 days from now as a unix timestamp
if($submitted < $min){
  die("Date is too early");
}

 

next would be inserting it into the DB right? what format should it be for the database? aka, if you manually wrote an INSERT statement, what would it be like?

 

Link to comment
Share on other sites

Hi Aaron,

Well it's something like this now, I got side tracked with attempting to use a php class calendar picker...

 

so I have this so far (not with the attempt at the use of the class)

<td align="left" nowrap><div class="CaptionReq"><B>*</B>Start Date:<br>Can NOT <br>be before <br>this date: <br>
                      <?php 
                      // $adstart  = date("d-M-Y", mktime( 0,0,0, date("m") , date("d")+2, date("Y")));
                      
              
                        $adstartstamp  = date("d-M-Y", mktime( 0,0,0, date("m") , date("d")+2, date("Y")));
					    $adstart  = date("d-M-Y", $adstartstamp);
                        $StartMonth = date("m",$adstartstamp);
                        $StartDay = date("d",$adstartstamp);
                        $StartYear = date("Y",$adstartstamp);  
                         //echo  ("$adstartstamp"); 
					 echo ("$StartMonth");
					 echo ("$StartDay");
					 echo ("$StartYear");
?>
                       </div></td>
					<td align="left">
						<SELECT NAME="StartMonth">
							<OPTION value="0">----MONTH----</OPTION>
							<OPTION VALUE="01">January</OPTION>
							<OPTION VALUE="02">February</OPTION>
							<OPTION VALUE="03">March</OPTION>
							<OPTION VALUE="04">April</OPTION>

as you can see the Unix box time is still on there I haven't tried the DarkFreak's suggestion yet...was working on yet my other stuck spot lol ...

Link to comment
Share on other sites

the section of PHP code won't work. let me try to explain why:

<?php
//In the following line, mktime() produces a unix timestamp for 2 days from now
//That timestamp is then passed to date(), which formats it and stores it into $adstartstamp
$adstartstamp  = date("d-M-Y", mktime( 0,0,0, date("m") , date("d")+2, date("Y")));
//$adstartstamp now contains the string "25-Feb-2009"

//The next few lines all use date(), which as stated before expects a unix timestamp as a second argument
//But, you are passing it the string "25-Feb-2009", so it will fail
$adstart  = date("d-M-Y", $adstartstamp);
$StartMonth = date("m",$adstartstamp);
$StartDay = date("d",$adstartstamp);
$StartYear = date("Y",$adstartstamp);  
?>

 

do you see the problem now?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.