PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
eyespark
Forum Commoner
Posts: 50 Joined: Tue Jan 24, 2006 7:36 am
Post
by eyespark » Thu Apr 06, 2006 9:05 am
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
Last edited by
eyespark on Thu Apr 06, 2006 4:53 pm, edited 1 time in total.
Maugrim_The_Reaper
DevNet Master
Posts: 2704 Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland
Post
by Maugrim_The_Reaper » Thu Apr 06, 2006 9:14 am
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?
eyespark
Forum Commoner
Posts: 50 Joined: Tue Jan 24, 2006 7:36 am
Post
by eyespark » Thu Apr 06, 2006 9:21 am
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
timvw
DevNet Master
Posts: 4897 Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium
Post
by timvw » Thu Apr 06, 2006 10:58 am
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)
eyespark
Forum Commoner
Posts: 50 Joined: Tue Jan 24, 2006 7:36 am
Post
by eyespark » Thu Apr 06, 2006 12:00 pm
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]
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Thu Apr 06, 2006 1:28 pm
I'm getting REALLY tired of editing your posts eyespark.
eyespark
Forum Commoner
Posts: 50 Joined: Tue Jan 24, 2006 7:36 am
Post
by eyespark » Thu Apr 06, 2006 1:43 pm
Thank you
timvw
DevNet Master
Posts: 4897 Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium
Post
by timvw » Thu Apr 06, 2006 3:41 pm
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).
eyespark
Forum Commoner
Posts: 50 Joined: Tue Jan 24, 2006 7:36 am
Post
by eyespark » Thu Apr 06, 2006 4:53 pm
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
timvw
DevNet Master
Posts: 4897 Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium
Post
by timvw » Thu Apr 06, 2006 5:01 pm
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.