# [web] Trying to display the current time in PHP

This topic is 4691 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've been learning a bit of basic PHP, and I've hit a snag (again). I've got some code to display the current time, but it isn't working correctly. Here's my index.php code:
<?php
include "includes/time.inc.php";
$timeOutput = new time; ?> <font color=black> <?$timeOutput->generateTime(); ?>
<? $timeOutput->displayTime(); ?> </font>  And here's the file "time.inc.php":  <? class time { var$time;

function generateTime() {

$time = getDate(); foreach ($time as $key =>$value) {

print $key.": ".$value."<BR>";

}

}

function displayTime() {

print ($time['weekday'].", ".$time['mday']." ".$time['month']." ".$time['year']);

}

}
?>


But here's what gets output:
seconds: 34
minutes: 15
hours: 21
mday: 18
wday: 0
mon: 12
year: 2005
yday: 351
weekday: Sunday
month: December
0: 1134940534
,

Where the comma is, you can see from the code it should say something like "Sunday, 18 December 2005" - but it doesn't, I just see the comma. The time is obviously being generated fine as it's being printed out by the generateTime() method. So where am I going wrong? Does PHP treat variables in a different way, or something? Thanks for your help, ukdeveloper.

##### Share on other sites
PHP class variables must be accessed with "$this" at all times.$this->time = getDate();

Have you considered just using the "date" function?

print date('Y-m-d');

##### Share on other sites
Yes, date(). When programming in php it is important to remember they have a function for pretty much everything. Check php.net before implimenting something like that.

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632988
• Total Posts
3009738
• ### Who's Online (See full list)

There are no registered users currently online

×