Page 1 of 1
Time problem
Posted: Thu Apr 06, 2006 9:05 am
by eyespark
Hi
If I put this code
Code: Select all
<?
$post_date = date(U);
echo date("d.m.Y @ H:i",$post_date);
?>
on my server, the date and time are shown correctly.
But if I store $post_date in mysql and then call it like this
Code: Select all
$post_date = $row['timestamp'];
$comm_date = date("d.m.Y @ H:i",$post_date);
echo $comm_date;
there is a 2 hour diference.
Why? And what would be a solution?
Thanks
Posted: Thu Apr 06, 2006 9:14 am
by Maugrim_The_Reaper
Print out the timestamp before and after storage. Maybe use time() in place of date('U')?
Does your PHP version by any chance do some weird timezone mechanics - thinking here of PHP 5.1 which added a few new date functions?
Posted: Thu Apr 06, 2006 9:21 am
by eyespark
I have done all that. Timestamp that is stored in mysql is correct. I'm pulling out my hair...
My PHP is 4..4.2
Thanks
Re: Time problem
Posted: Thu Apr 06, 2006 10:58 am
by timvw
Show us the real code please...
date(U) isn't really valid (From the manual: string date ( string format [, int timestamp] ))
And where does $row['timestamp'] come from?
(Most obvious problem is a timezone configuration problem)
Posted: Thu Apr 06, 2006 12:00 pm
by eyespark
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Code: Select all
<?
function comments() {
$articles = mysql_query("SELECT * FROM articles WHERE aid='". $_GET['aid'] ."'") or die(mysql_error());
$row_articles = mysql_fetch_assoc($articles);
$totalRows_articles = mysql_num_rows($articles);
if (isset($_POST['sub_comments'])) {
$articles = mysql_query("SELECT * FROM articles WHERE aid='". $_POST['aid'] ."'") or die(mysql_error());
$timestamp = date(U);
mysql_query("INSERT INTO comments (aid, name, email, website, content, timestamp) VALUES ('". $_POST['aid'] ."', '". ucwords($_POST['name']) ."', '". $_POST['email'] ."', '". $_POST['website'] ."', '". format_textarea(strip_tags(ucfirst(trim_end($_POST['content'])))) ."', '$timestamp')") or die (mysql_error());
echo "<script>self.location='". $_POST['aid'] ."-a.html';</script>";
}
$comments = mysql_query("SELECT * FROM comments WHERE aid='". $_GET['aid'] ."' ORDER BY timestamp ASC") or die(mysql_error());
$all_comments = mysql_num_rows($comments);
$row_comm = mysql_fetch_assoc($comments);
if ($all_comments >= 1) {
echo ("<h2>Number of comments: $all_comments</h2>");
do {
$cas = $row_comm['timestamp'];
$comm_date = date("d.m.Y @ H:i",$cas);
$author = $row_comm['name'];
$aid = $row_comm['aid'];
$commid = $row_comm['commentid'];
echo ("<div>
<p class=\"komdatum\">$author ~ $comm_date</p>
$row_comm[content]
</div>");
} while ($row_comm = mysql_fetch_assoc($comments));
} else {
echo ("<p><strong>No comments yet.</strong></p>");
}
// here comes only form for submiting comments
}
?>
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Thu Apr 06, 2006 1:28 pm
by feyd
I'm getting REALLY tired of editing your posts eyespark.

Posted: Thu Apr 06, 2006 1:43 pm
by eyespark
Thank you
Posted: Thu Apr 06, 2006 3:41 pm
by timvw
I'll repeat my question... In which timezone is your webserver?
Although it seems pretty much undefined behaviour (U is an undefined constant in your date(U) call) it appears that you get the current local time... Depending on the configuration of your webserver this may differ with your time... (Off-topic: I would expect a webserver to use GMT).
Posted: Thu Apr 06, 2006 4:53 pm
by eyespark
I found where my problem was. You are correct: I was getting GMT. My time is GMT + 2 hours.
Um, but I don't know what you mean by "U is an undefined constant in your date(U) call".
If I understand correctly, date(U) = time().
Please correct me if I'm wrong.
Thanks
Posted: Thu Apr 06, 2006 5:01 pm
by timvw
eyespark wrote:
If I understand correctly, date(U) = time().
That's the way it seems to be, but it's undefined behaviour, read: you can't be sure it's really that.
As the documentation for
date says:
string date ( string format [, int timestamp] )
Returns a string formatted according to the given format string using the given integer timestamp or the current local time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time().
Instead of relying on some undefined behaviour i suggest you simply don't use the 'U' as a parameter.