What's new

Web [PHP][SOLVED] Help with Unix Time? Hour seems to be off.

  • Thread starter Triiistan
  • Start date
  • Views 2,326
T

Triiistan

Contributor
Messages
1,958
Reaction score
495
Hey guys, I have been working on a reminder system in PHP and I am having some trouble with Unix Time. I am using the function mktime(), however, the hour always seems to be off by one. If I enter 3:30 PM, it will give me 4:30 PM. Also, it is not done yet, I am currently echo'ing out the unix time variable $datetime to see if everything matches up before I continue. Any idea on how I can fix this?

reminder.php
PHP:
<?php
require 'connect.inc.php';
 
$firstname = getuserfield('firstname');
$lastname = getuserfield('lastname');
$email = getuserfield('email');
echo 'You\'re logged in, '.$firstname.' '.$lastname.'. <a href="logout.php">Log out</a> <a href="settings.php">Settings</a>';
 
if(isset($_POST['month']) && isset($_POST['day']) && isset($_POST['year']) && isset($_POST['hour']) && isset($_POST['minute']) && isset($_POST['is_pm']) && isset($_POST['subject']) && isset($_POST['message'])){
 
$month = $_POST['month'];
$day = $_POST['day'];
$year = $_POST['year'];
$hour = $_POST['hour'];
$minute = $_POST['minute'];
$is_pm = $_POST['is_pm'];
$subject = $_POST['subject'];
$message = $_POST['message'];
 
if($is_pm==1){
    $hour += 12;
}
$datetime = mktime($hour, $minute, 0, $month, $day, $year);
echo $datetime;
 
    if(strlen($day)>2 || strlen($hour)>2 || strlen($minute)>2 || strlen($subject)>90 || strlen($message)>140){
        echo 'Please adhere to the max length of fields.';
    } else {
        echo 'ok.';
    }
 
}
 
 
?>
<!DOCTYPE html>
<html>
 
<head>
<title>Reminder System</title>
</head>
 
<body>
 
<h1>Set Reminder</h1>
<form action="#" method="POST">
Date:<br><select name="month" autofocus required>
            <option value="" style="display:none;"></option>;
            <option value="1">January</option>
            <option value="2">February</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>
        <input type="text" name="day" size="2" maxlength="2" required>,
        <select name="year" required>
            <option value="" style="display:none;"></option>
            <option value=2013>2013
            <option value=2014>2014
            <option value=2015>2015
            <option value=2016>2016
            <option value=2017>2017
            <option value=2018>2018
            <option value=2019>2019
            <option value=2020>2020
            <option value=2021>2021
            <option value=2022>2022
            <option value=2023>2023
            <option value=2024>2024
            <option value=2025>2025
            <option value=2026>2026
            <option value=2027>2027
            <option value=2028>2028
            <option value=2029>2029
            <option value=2030>2030
            <option value=2031>2031
            <option value=2032>2032
            <option value=2033>2033
            <option value=2034>2034
            <option value=2035>2035
            <option value=2036>2036
            <option value=2037>2037
            <option value=2038>2038
        </select><br><br>
Time:<br><input    type="text" name="hour"  size="2" maxlength="2" required>:<input type="text" name="minute" size="2" maxlength="2" required>
<select name="is_pm" size="1" required>
    <option value="" style="display:none;"></option>
    <option value="0">AM
    <option value="1">PM
</select><br><br>
 
Subject:<br><input type="text" name="subject" maxlength="90" required><br><br>
Message:<br><textarea rows="4" cols="30" name="message" maxlength="140" required></textarea><br><br>
<input type="submit" style="width:265px; height:70px;" value="Save">
</form>
 
</body>
</html>
 
W

Wheen

Bytesexual
Retired
Messages
6,154
Reaction score
2,342
Have you accounted for daylight savings (On your actual clock or in the script)
 
T

Triiistan

Contributor
Messages
1,958
Reaction score
495
Have you accounted for daylight savings (On your actual clock or in the script)
Well yes. Kind of.

I found this in PHP documentation.
Note:
As of PHP 5.1.0, this parameter became deprecated. As a result, the new timezone handling features should be used instead.

Would you know how to use timezone handling features with mktime()?

Thanks! :smile:
 
V

Visual Studio

The Original Shiba Inu
Messages
2,758
Reaction score
1,486
Well yes. Kind of.

I found this in PHP documentation.
Note:
As of PHP 5.1.0, this parameter became deprecated. As a result, the new timezone handling features should be used instead.

Would you know how to use timezone handling features with mktime()?

Thanks! :smile:
Set your timezone in your php/php5.ini file like this:
Code:
date.timezone = "America/New_York"
Here is a list of the timezones.
 
D

Dark TwizTid

1 + 0 = 10
Messages
2,129
Reaction score
583
since that is covered, you should talk to someone about "sanitizing" your inputs :smile: it is dangerous to have $_POST["blahblah"] if you plan on inserting those into a database :smile:
 
V

Visual Studio

The Original Shiba Inu
Messages
2,758
Reaction score
1,486
since that is covered, you should talk to someone about "sanitizing" your inputs :smile: it is dangerous to have $_POST["blahblah"] if you plan on inserting those into a database :smile:
Yeah he needs to clean his inputs.
$firstname = getuserfield('firstname');$lastname = getuserfield('lastname');
Hmmm what happens to the echo when is use ?><?php echo("ohai"); ?><?php as my first name or last name? :wink:
 
Last edited:
D

Dark TwizTid

1 + 0 = 10
Messages
2,129
Reaction score
583
To start off, without getting into anything advanced, and Visual Studio can probably confirm or deny if this is the correct way to do it.

PHP:
$month = mysql_real_escape_string($_POST['month']);

which today is still open to attacks, but gets security up a notch at least :tongue:
 
V

Visual Studio

The Original Shiba Inu
Messages
2,758
Reaction score
1,486
To start off, without getting into anything advanced, and Visual Studio can probably confirm or deny if this is the correct way to do it.

PHP:
$month = mysql_real_escape_string($_POST['month']);

which today is still open to attacks, but gets security up a notch at least :tongue:
None of the dates are open to attacks because the only thing being echoed is the mktime so if you try to attack the month POST variable using a string (PHP, HTML, etc.) when it is expected to be an int for mktime it will just give you a script error. "firstname" and "lastname" are what will really get him. I would run all of the time inputs through is_int and filter code snippets from the firstname and lastname inputs with filter_var.
 
Last edited:
T

Triiistan

Contributor
Messages
1,958
Reaction score
495
None of the dates are open to attacks because the only thing being echoed is the mktime so if you try to attack the month POST variable using a string (PHP, HTML, etc.) when it is expected to be an int for mktime it will just give you a script error. "firstname" and "lastname" are what will really get him. I would run all of the time inputs through is_int and filter code snippets from the firstname and lastname inputs with filter_var.
Yeah, I definitely need to clean things up. However, I am just worrying about getting it right first, then I will filter it all.
 
D

Dark TwizTid

1 + 0 = 10
Messages
2,129
Reaction score
583
None of the dates are open to attacks because the only thing being echoed is the mktime so if you try to attack the month POST variable using a string (PHP, HTML, etc.) when it is expected to be an int for mktime it will just give you a script error. "firstname" and "lastname" are what will really get him. I would run all of the time inputs through is_int and filter code snippets from the firstname and lastname inputs with filter_var.

Ah right you are lol just now noticed that
 
S

Sumo

ヽ༼ຈل͜ຈ༽ノ raise your dongers ヽ༼ຈل͜ຈ༽ノ
VIP
Retired
Messages
5,499
Reaction score
3,443
since that is covered, you should talk to someone about "sanitizing" your inputs :smile: it is dangerous to have $_POST["blahblah"] if you plan on inserting those into a database :smile:

Not at all. Infact you should have $var = $_POST['something'], and then sanitize it ONLY when you go to insert it into the DB/output it on the page.

Yeah he needs to clean his inputs.
$firstname = getuserfield('firstname');$lastname = getuserfield('lastname');
Hmmm what happens to the echo when is use ?><?php echo("ohai"); ?><?php as my first name or last name? :wink:

Absolutely nothing....
 
V

Visual Studio

The Original Shiba Inu
Messages
2,758
Reaction score
1,486
Not at all. Infact you should have $var = $_POST['something'], and then sanitize it ONLY when you go to insert it into the DB/output it on the page.



Absolutely nothing....
Well you can still use some HTML/JS in there :/
 
V

Visual Studio

The Original Shiba Inu
Messages
2,758
Reaction score
1,486
Thanks for the help, however, I still keep getting the wrong unix time by an hour off. Its really weird. I would type in 4:30 and get 15:30 instead.
You set your timezone in your php/php5.ini file right?
 
T

Triiistan

Contributor
Messages
1,958
Reaction score
495
You set your timezone in your php/php5.ini file right?
Yup, I set it to the time zone you gave me, since I am in the EST timezone.

EDIT: This is an excerpt from my php.ini file.

PHP:
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = America/New_York
 
V

Visual Studio

The Original Shiba Inu
Messages
2,758
Reaction score
1,486
Yup, I set it to the time zone you gave me, since I am in the EST timezone.

EDIT: This is an excerpt from my php.ini file.

PHP:
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = America/New_York
Some hosts use php.ini files and some use php5.ini files so you might want to try both and see which works.
 
R

Revvv

Getting There
Messages
1,924
Reaction score
290
just add this at the top of your php file;
PHP:
date_default_timezone_set('GMT'); //your timezone
 
T

Triiistan

Contributor
Messages
1,958
Reaction score
495
just add this at the top of your php file;
PHP:
date_default_timezone_set('GMT'); //your timezone
Thanks, it works now! It gave me the correct time-stamp! :biggrin:

Visual Studio : Since I have the timestamp working correctly, all I need to create is that script for the cron job right?
 
Last edited:
R

Revvv

Getting There
Messages
1,924
Reaction score
290
Thanks, it works now! It gave me the correct time-stamp! :biggrin:
Haha no problem, I was going to post it the other day but I thought you had it sorted haha :tongue:

oh god i just read the title hahahahhahaa
 
Last edited:
Top Bottom