CodeMama Posted February 23, 2009 Share Posted February 23, 2009 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...? Quote Link to comment https://forums.phpfreaks.com/topic/146526-timedate-formatting/ Share on other sites More sharing options...
rhodesa Posted February 23, 2009 Share Posted February 23, 2009 $adstartstamp = mktime( 0,0,0, date("m") , date("d")+2, date("Y")); $startmonth = date("m",$adstartstamp); $startday = date("d",$adstartstamp); $startyear = date("Y",$adstartstamp); Quote Link to comment https://forums.phpfreaks.com/topic/146526-timedate-formatting/#findComment-769255 Share on other sites More sharing options...
CodeMama Posted February 23, 2009 Author Share Posted February 23, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/146526-timedate-formatting/#findComment-769275 Share on other sites More sharing options...
rhodesa Posted February 23, 2009 Share Posted February 23, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/146526-timedate-formatting/#findComment-769281 Share on other sites More sharing options...
CodeMama Posted February 23, 2009 Author Share Posted February 23, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/146526-timedate-formatting/#findComment-769289 Share on other sites More sharing options...
darkfreaks Posted February 23, 2009 Share Posted February 23, 2009 im sure you can edit this codemama: <?php $today = date("F j, Y, g:i a"); // March 10, 2001, 5:16 pm ?> Quote Link to comment https://forums.phpfreaks.com/topic/146526-timedate-formatting/#findComment-769327 Share on other sites More sharing options...
premiso Posted February 23, 2009 Share Posted February 23, 2009 Since no one pointed it out really, date the PHP Manual for date tells you all the variables you can use and what they will return. Quote Link to comment https://forums.phpfreaks.com/topic/146526-timedate-formatting/#findComment-769329 Share on other sites More sharing options...
rhodesa Posted February 23, 2009 Share Posted February 23, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/146526-timedate-formatting/#findComment-769333 Share on other sites More sharing options...
CodeMama Posted February 23, 2009 Author Share Posted February 23, 2009 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 ... Quote Link to comment https://forums.phpfreaks.com/topic/146526-timedate-formatting/#findComment-769433 Share on other sites More sharing options...
rhodesa Posted February 23, 2009 Share Posted February 23, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/146526-timedate-formatting/#findComment-769440 Share on other sites More sharing options...
CodeMama Posted February 23, 2009 Author Share Posted February 23, 2009 I see the problem but not the solution, and I have read (and read again) the php.net section about the date() function... seems like there should be a way to say $startmonth = $adstart ("M") or something to that effect Quote Link to comment https://forums.phpfreaks.com/topic/146526-timedate-formatting/#findComment-769484 Share on other sites More sharing options...
rhodesa Posted February 23, 2009 Share Posted February 23, 2009 change $adstartstamp = date("d-M-Y", mktime( 0,0,0, date("m") , date("d")+2, date("Y"))); to $adstartstamp = mktime( 0,0,0, date("m") , date("d")+2, date("Y")); Quote Link to comment https://forums.phpfreaks.com/topic/146526-timedate-formatting/#findComment-769504 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.