/    Sign up×
Community /Pin to ProfileBookmark

Date/Time Display Issue

Hi all,

I log the date and time people log into my site, I was able to display this using this code

[CODE]<?php echo $qry[‘loginDateTime’]; ?>[/CODE]

, but I wanted to change the way the date and time were displayed so I added this

[CODE]<?php echo date(‘d/m/Y H:i:s’, strtotime($qry[‘loginDateTime’])); ?>[/CODE]

, that fixed the issue but now something wierd is happening. Not many members have logged in since I added the loginDateTime column in my db. So with the first code it just displayed nothing for members that haven’t logged and that is fine. But with the new code if they haven’t logged in it populates the field anyway with today’s date and a time of 00:00:00… Any ideas as to why this is happening?

to post a comment
PHP

19 Comments(s)

Copy linkTweet thisAlerts:
@bokehMar 13.2007 — From the manual (a very valuable resource):

[B]strtotime:[/B][I]The function expects to be given a string containing a US English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT), relative to the timestamp given in now, [B]or the current time if none is supplied[/B].[/I]
Copy linkTweet thisAlerts:
@tomyknokerauthorMar 13.2007 — So then is there no way around it? All the members who haven't logged in are getting this as there loginDateTime result '14/03/2007 00:00:00'...
Copy linkTweet thisAlerts:
@bokehMar 13.2007 — [code=php]<?php echo($qry['loginDateTime']?date('d/m/Y H:i:s', strtotime($qry['loginDateTime'])):'unknown') ?>[/code]
Copy linkTweet thisAlerts:
@tomyknokerauthorMar 13.2007 — You're too good! I have been trying to figure it out all night! Thanks it's a beautiful thing ?
Copy linkTweet thisAlerts:
@tomyknokerauthorMar 13.2007 — How come I don't need to put a ';' after it?
Copy linkTweet thisAlerts:
@bokehMar 13.2007 — "[I][B];[/B][/I]" is php's delimiter and since there is nothing to delimit there is no need for a delimiter.
Copy linkTweet thisAlerts:
@NightShift58Mar 13.2007 — From the manual (a very valuable resource):

[B]strtotime:[/B][I]The function expects to be given a string containing a US English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT), relative to the timestamp given in now, [B]or the current time if none is supplied[/B].[/I][/QUOTE]
That wouldn't explain the "H:i:s" of 00:00:00 because either it would display the entire 1970-01-01 date/time or the entire current date/time but never a mixture of both, i.e. today's date and 1970-01-01's time.

The problem is likely found in the table's field definition, i.e. the retrieved content of $qry['loginDateTime'], which seems not to include an H:i:s component, hence 00:00:00.
Copy linkTweet thisAlerts:
@bokehMar 13.2007 — Well I thought it was weird so I give it an empty string and it returned false. Give it no argument and it returns an error, but the column must be returning something that evaluates to false otherwise the code I posted wouldn't work. Very strange!
Copy linkTweet thisAlerts:
@NightShift58Mar 13.2007 — You made a comment a few days ago about the use of strtotime(). I think this affirms your reluctance to use it. It's a "quirks mode" function, that one should only use when there is certainty that the parameters are well formed.
Copy linkTweet thisAlerts:
@tomyknokerauthorMar 14.2007 — Hi guys, well I'm happy for now it works. But should I change something in the database in the future, is that what you're saying?
Copy linkTweet thisAlerts:
@bokehMar 14.2007 — I'll let NightShift answer that one, I'm going to bed.
Copy linkTweet thisAlerts:
@tomyknokerauthorMar 14.2007 — Bed oh man but I just posted in another post you were looking into me for! ?
Copy linkTweet thisAlerts:
@NightShift58Mar 14.2007 — Yes. No. Maybe.

How is that field (loginDateTime) defined?
Copy linkTweet thisAlerts:
@tomyknokerauthorMar 14.2007 — It's defined as DATETIME, and it's default is NULL
Copy linkTweet thisAlerts:
@NightShift58Mar 14.2007 — What length? 6, 8, 12 or 14?
Copy linkTweet thisAlerts:
@tomyknokerauthorMar 14.2007 — Thw type is just datetime with no number,,,
Copy linkTweet thisAlerts:
@bokehMar 14.2007 — [code=php]<?php

echo date('Y-m-d H:i:s', strtotime('0000-00-00 00:00:00')); // 1999-11-30 00:00:00

echo date('Y-m-d H:i:s', strtotime(0)); // 2007-03-14 00:00:00

echo date('Y-m-d H:i:s', strtotime('0')); // 2007-03-14 00:00:00

echo date('Y-m-d H:i:s', strtotime(null)); // Warning: strtotime() [function.strtotime]: Called with an empty time parameter.

echo date('Y-m-d H:i:s', strtotime('')); // Warning: strtotime() [function.strtotime]: Called with an empty time parameter.

?>[/code]
Copy linkTweet thisAlerts:
@tomyknokerauthorMar 15.2007 — [code=php]<?php

echo date('Y-m-d H:i:s', strtotime('0000-00-00 00:00:00')); // 1999-11-30 00:00:00

echo date('Y-m-d H:i:s', strtotime(0)); // 2007-03-14 00:00:00

echo date('Y-m-d H:i:s', strtotime('0')); // 2007-03-14 00:00:00

echo date('Y-m-d H:i:s', strtotime(null)); // Warning: strtotime() [function.strtotime]: Called with an empty time parameter.

echo date('Y-m-d H:i:s', strtotime('')); // Warning: strtotime() [function.strtotime]: Called with an empty time parameter.

?>[/code]
[/QUOTE]
That's a good resource thanks! Ok well I added a sort function to my page and as a result, (I did it on a tutorial) I wasn't sure where I couls put the 'strtotime', If you could give me a clue would be great
[code=php]
/* set the allowed order by columns */
$default_sort = 'LastName';
$allowed_order = array ('JoinDate', 'FirstName','LastName', 'loginDateTime');

/* if order is not set, or it is not in the allowed
* list, then set it to a default value. Otherwise,
* set it to what was passed in. */
if (!isset ($_GET['order']) ||
!in_array ($_GET['order'], $allowed_order)) {
$order = $default_sort;
} else {
$order = $_GET['order'];
}

/* construct and run our query */
$query = "SELECT * FROM tblmembers WHERE MemberApproved='$cat' ORDER BY $order";

$result = mysql_query ($query);

/* make sure data was retrieved */
$numrows = mysql_num_rows($result);
if ($numrows == 0) {
echo "No data to display!";
exit;
}

/* now grab the first row and start the table */
$row = mysql_fetch_assoc ($result);
echo "<TABLE border=1>n";
echo "<TR>n";
foreach ($row as $heading=>$column) {
/* check if the heading is in our allowed_order
* array. If it is, hyperlink it so that we can
* order by this column */
echo "<TD><b>";
if (in_array ($heading, $allowed_order)) {
echo "<a href="{$_SERVER['PHP_SELF']}?order=$heading&cat=$cat">$heading</a>";
} else {
echo $heading;
}

echo "</b></TD>n";
}
echo "</TR>n";

/* reset the $result set back to the first row and
* display the data */
mysql_data_seek ($result, 0);
while ($row = mysql_fetch_assoc ($result)) {
echo "<TR>n";
foreach ($row as $column) {
echo "<TD>$column</TD>n";
}
echo "</TR>n";
}
echo "</TABLE>n";
?>
[/code]
Copy linkTweet thisAlerts:
@NightShift58Mar 15.2007 — Thw type is just datetime with no number,,,[/QUOTE]You should assign it a length of 14 to get the full benefits. That way it would always return yyy-mm-dd hh:mm:ss and you won't get 00:00:00 in your datetime string.
×

Success!

Help @tomyknoker spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.22,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...