Hi, How can i return the server date? using

<?php $my_t=getdate(date("U")); 
print("$my_t[weekday], $my_t[month] $my_t[mday], $my_t[year]"); ?>

gives me errors :(

Thanks in advance, I know it probably has a very easy solution, but I just dont know what as I'm so totally new to php..

Dani AI

Generated

A few quick, practical notes that build on what , , , and suggested.

The most common immediate cause of the kind of error shown in the original snippet is a PHP parsing/variable interpolation issue (PHP treating an unquoted array key inside a double-quoted string as a constant). Another frequent cause is that the file is not being executed by PHP at all (wrong extension or opened via file://). Turn on error reporting first so you see the exact problem:

<?php
ini_set('display_errors', '1');
error_reporting(E_ALL);
?>

For a clearer, timezone-aware approach use PHP's DateTime API rather than manually assembling getdate arrays; it avoids interpolation pitfalls and makes formatting/timezone handling straightforward:

<?php
date_default_timezone_set('UTC'); // set to your zone if needed
$dt = new DateTime();
echo $dt->format('l, F j, Y H:i:s');
?>

If you cannot change php.ini on your host, date_default_timezone_set() inside your script sets the runtime timezone. For account- or server-wide changes you need to edit php.ini or use hosting controls (some hosts allow .htaccess, some do not). See the official docs for details: DateTime class and date_default_timezone_set.

Checklist:

  • Enable errors and read the message.
  • Ensure the file has a .php extension and is accessed through your web server (http://).
  • If the error mentions an undefined constant, fix how you reference array elements (use braces/quotes or concatenate), or switch to DateTime as above.
  • Set timezone explicitly when you need server time to match a specific zone.

Recommended Answers

All 5 Replies

Try this

<?
$day=getdate();
echo($day['wday']."/".$day['mon'])."/".$day['year']);
?>

Hi,

I am sharing a tip with you.

Sometimes our server shows a different time than the time shown on our clock, the most probable reason is that our hosting server is in a different time-zone.

We can easily set our server time to our time zone changing a setting in PHP called date.timezone.
In order to set the new time in every file, we need not update the new time in all the files, all that we would need to do is replace a single line in htaccess file.

To set a time zone for your entire account or directory, create a .htaccess file in the upper directory with the line:

Hope you find this useful and of assistance.

Thanks,
Suryakant

You may write like this also.
<?php print date("Y-m-d H:i:s"); ?>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.