Jump to content

getting wrong results with date('c', mktime())


leke

Recommended Posts

I'm practising how to do date and time in PHP. I wanted to see if I can get all of the date/time variables from user submission and format them correctly.

 

I went with ISO 8601 date...

// From the Manual:

// date('c', mktime(1, 2, 3, 4, 5, 2006));

// Prints something like: 2006-04-05T01:02:03+00:00

 

...but my attempts always produce a slightly different result for the year, month and timezone...

$inputTime = date('c', mktime((int)$timeHour, (int)$timeMins, (int)$timezone, (int)$dateMonth, (int)$dateDay, (int)$dateYear));

echo $inputTime;
// Should return:    2013-11-02T02:30:01+00:00
// Instead returns: 2012-12-02T02:30:01-08:00

The timezone value coming from HTML...

<option value="1.0">(GMT +1:00)</option>

 

I noticed;

1. It always picks a year less than whatever year I select.

2. It always selects 12 as the month.

3. It always adds -08:00 after the picking the correct timezone.

 

How come? :confused:

 

Thanks in advance.

 

Link to comment
https://forums.phpfreaks.com/topic/233707-getting-wrong-results-with-datec-mktime/
Share on other sites

Hi, Here are the problem parts of the form...

 

<form action="contact_db/contact_submit_script.php" method="post">
<p>Time and date</p>

<select name="dateMonth">
<option>Month</option>
<option value="1">January</option>
<option value="2">Febuary</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>

</select>

<select name="dateYear">
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
</select>

<select name="timezone" id="timezone">
      <option>TimeZone</option>
      <option value="-12.0">(GMT -12:00)</option>
      <option value="-11.0">(GMT -11:00)</option>
      <option value="-10.0">(GMT -10:00)</option>
      <option value="-9.0">(GMT -9:00)</option>
      <option value="-8.0">(GMT -8:00)</option>
      <option value="-7.0">(GMT -7:00)</option>
      <option value="-6.0">(GMT -6:00)</option>
      <option value="-5.0">(GMT -5:00)</option>
      <option value="-4.0">(GMT -4:00)</option>
      <option value="-3.5">(GMT -3:30)</option>
      <option value="-3.0">(GMT -3:00)</option>
      <option value="-2.0">(GMT -2:00)</option>
      <option value="-1.0">(GMT -1:00)</option>
      <option value="0.0">(GMT)</option>
      <option value="1.0">(GMT +1:00)</option>
      <option value="2.0">(GMT +2:00)</option>
      <option value="3.0">(GMT +3:00)</option>
      <option value="3.5">(GMT +3:30)</option>
      <option value="4.0">(GMT +4:00)</option>
      <option value="4.5">(GMT +4:30)</option>
      <option value="5.0">(GMT +5:00)</option>
      <option value="5.5">(GMT +5:30)</option>
      <option value="5.75">(GMT +5:45)</option>
      <option value="6.0">(GMT +6:00)</option>
      <option value="7.0">(GMT +7:00)</option>
      <option value="8.0">(GMT +8:00)</option>
      <option value="9.0">(GMT +9:00)</option>
      <option value="9.5">(GMT +9:30)</option>
      <option value="10.0">(GMT +10:00)</option>
      <option value="11.0">(GMT +11:00)</option>
      <option value="12.0">(GMT +12:00)</option>
</select>
</p>

<input type="submit" name="submit" value="Send message" />
</form>

 

The PHP code.

I corrected the 3rd attribute of mktime().

$message = $_POST['message'];
$message_br = nl2br($message);
$LocationType = $_POST['locationType'];
$dateDay = $_POST['dateDay'];
$dateMonth = $_POST['dateMonth'];
$dateYear = $_POST['dateYear'];
$timeHour = $_POST['timeHour'];
$timeMins = $_POST['timeMins'];
$timeAMPM = $_POST['timeAMPM'];
$userName = $_POST['name'];
$timezone = $_POST['timezone'];

$inputTime = date('c', mktime((int)$timeHour, (int)$timeMins, 0, (int)$dateMonth, (int)$dateDay, (int)$dateYear));
echo $inputTime;

 

I'm just reading through date_default_timezone_set() and seeing how I can add the timezone to the date()

To use date_default_timezone_set with your form you'd have to change the values to "+1", "+2", etc. and then prefix it with "Etc/GMT ".

date_default_timezone_set("Etc/GMT $timezone");

If you want Daylight Savings to work though, you'll have to use timezones like US/Eastern/Central/Pacific etc. See here for a list of supported timezones: http://www.php.net/manual/en/timezones.php

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.