Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/275896-passing-variable-into-datetime/
Share on other sites

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 by requinix

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.

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 by Semsem

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?

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.