Semsem Posted March 20, 2013 Share Posted March 20, 2013 Okay, let's get this straight off the bat: I hate manipulating time in PHP. I don't understand any but a little bit of it. Okay, so my problem is rather simple, I think. I have a form, where the use inputs the date variables. I then want to combine those individual variables into a date, and then export the date in UNIX time. $year = $_POST['year']; $month = $_POST['month']; $day = $_POST['day']; $hour = $_POST['hour']; $min = $_POST['min']; $sec = $_POST['sec']; $date = new DateTime(); $date->setDate($year, $month, $day); $date->setTime($hour, $min, $sec); Then, to export UNIX: $date->format("U"); Problem is, when all those variables instead of having a "$_POST" variable attached, but have a default manually inserted number, ie, $year = 2013; (and continued like that), it works just fine and I am able to get a UNIX time stamp for whatever date I manually code into the PHP. But, what I want to be able to do is have the user enter the various time variables from a form (without the variables being hard coded in), and have it do the same. If anyone understands what I am wanting to do, any assitance in this matter would be greatly appreciated. I've tried (unsuccessfully) using strtotime and mktime and DateTime and combinations of the three, to no avail. And, after around 4 hours experimenting and Googling, I have to admit I need help as I am getting nowhere. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/275896-passing-variable-into-datetime/ Share on other sites More sharing options...
requinix Posted March 20, 2013 Share Posted March 20, 2013 (edited) DateTime is nice and all but if all you want is the Unix timestamp I'd just go with mktime(). It should just be a matter of $unix = mktime($_POST["hour"], $_POST["minute"], $_POST["second"], $_POST["month"], $_POST["day"], $_POST["year"]);All those fields are numbers, right? Any errors? What does var_dump($_POST) produce? By the way, as with all user input you should validate it before trying to use it. Edited March 20, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/275896-passing-variable-into-datetime/#findComment-1419736 Share on other sites More sharing options...
Semsem Posted March 20, 2013 Author Share Posted March 20, 2013 Yes, they are number fields. When I do something like, $unix = mktime(0, 0, 0, 1, 1, 2015); it returns the value of 1420088400, which is proper, Jan 1, 2015 00:00:00, but when I do $unix = mktime(0, 0, 0, 1, 1, $_POST["year"]); it produces 943938000, which is Nov 30, 1999. The form does not work at all when I put var_dump($_POST); or var_dump(unix); or var_dump($_POST["year"]); To be honest, that's the first time I've ever heard of var_dump, which is very sad. Quote Link to comment https://forums.phpfreaks.com/topic/275896-passing-variable-into-datetime/#findComment-1419840 Share on other sites More sharing options...
Barand Posted March 20, 2013 Share Posted March 20, 2013 if you're having trouble with var_dump (which is used for debugging, not passing variables to functions) try this, it's more legible, to see what is in $_POST echo '<pre>', print_r($_POST, 1), '</pre>' ; Quote Link to comment https://forums.phpfreaks.com/topic/275896-passing-variable-into-datetime/#findComment-1419861 Share on other sites More sharing options...
Semsem Posted March 20, 2013 Author Share Posted March 20, 2013 (edited) Okay, it was working properly (the PHP code, anyway), I took it to a new page and it worked. So it means that the error is not necessarily the PHP like I had thought but something to do with JavaScript (which I also hate). HTML submit is as follows: <input class="czas" type="submit" onClick="closeFB();" name="newTime" value="Set Time" /> Currently, my function is as follows: <script> function closeFB() { <? $unix = mktime(0, 0, 0, 1, 1, $_POST["year"]);?> $("#startTimeDiv").html("<div id='message'></div>"); $('#message').html("<p>X</p>") .append("<p>X<br><? echo $unix; ?></p>") $.fancybox.close(); } </script> I'm also using Fancybox to open up the time form in a modal popup, have user enter the time, hit to Set the Time, have the modal popup close and update a couple divs with new info. It works fine when using <? $unix = mktime(0, 0, 0, 1, 1, 2015);?>, but the second I change 2015 to $_POST["year"], it then fails. The problem why var_dump and print_r would not work was due to JavaScript not being able to can't split a string across lines (it returned the error of unterminated string literal in FireBug), which is why I moved the code to a new page and isolated it, where it worked. But, I think this is a JavaScript problem, and I think it has to additionally be a PHP problem. Any help would be appreciated. Edited March 20, 2013 by Semsem Quote Link to comment https://forums.phpfreaks.com/topic/275896-passing-variable-into-datetime/#findComment-1419931 Share on other sites More sharing options...
Semsem Posted March 20, 2013 Author Share Posted March 20, 2013 Alright, I think this is mostly a JavaScript issue now. When <? $unix = mktime(0, 0, 0, 1, 1, 2015);?>, and I look at the page's source from the browser, I am able to see the unix timestamp in the function. When it it <? $unix = mktime(0, 0, 0, 1, 1, $_POST["year"]);?> there is no number in the page's source. So it appears as though my problem is that the code is running before it should (before any variables are entered). Any ideas on how to fix it? Quote Link to comment https://forums.phpfreaks.com/topic/275896-passing-variable-into-datetime/#findComment-1419944 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.