Jump to content
  • Advertisement
Sign in to follow this  
ukdeveloper

[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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!