6

I have a problem outputting the date which is created within my PHP file.

I've been following a tutorial on how to make a really Basic-CMS Platform to help me understand some of the basics for databases and PHP, everything has been going well up until I was trying to output the date of when the page was created.

This is the error I get

Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): It is not safe to rely on the system's timezone settings. 
You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of
those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone 'UTC' for now, but please set date.timezone to select your timezone.' in 
C:\MAMP\htdocs\basic-cms\page.php:22 Stack trace: #0 C:\MAMP\htdocs\basic-cms\page.php(22): DateTime->__construct('2016-02-17 10:3...') #1 {main} thrown in C:\MAMP\htdocs\basic-cms\page.php on line 22

Now when I remove line 22 inside my page.php it outputs the full date that is inside the database i.e 2016-02-17 10:38:05 but I'm trying to format it to show the date like jS M, Y (17th Feb 2016).

This is the code inside my page.php file

if ( $page ) {
    $page['created'] = new DateTime( $page['created'] );

    if ( $page['updated'] ) {
        $page['updated'] = new DateTime( $page['created'] );
    }
}

Then inside my show.php where I'm displaying this I have this code to format the date.

Created on <?php echo $page['created']->format('jS M, Y'); ?>

Now removing this from my show.php doesn't do anything as that's not where the error is contained - but I thought I'd show you guys what I am trying to achieve.

Like I said this is a very Basic-CMS site that I am creating by following a tutorial on YouTube and I have copied his code exactly and he got no error so I'm sure it has to be a typo I just can't find.

4
  • had you tried to set time zone? Commented Feb 17, 2016 at 11:36
  • How would I go about trying to set a time zone? In his Tutorial he doesn't set a time zone from what I can see unless he has done it in the background before he started the video Commented Feb 17, 2016 at 11:37
  • this is because in your fatal error it's there, its clearly showing its issue with timezone Commented Feb 17, 2016 at 11:39
  • 1
    The message says that you have two options:a) set the config option date.timezone, e.g. in the php.ini or b) use date_default_timezone_set prior to creating the datetime instances. Commented Feb 17, 2016 at 11:41

2 Answers 2

15

This is because no timezone has set in php configuration.

Add the following line of code to top of your php.ini file

date.timezone = "US/Central"

And restart web server

OR

you can set it via php script also by using following function:

date_default_timezone_set('America/Los_Angeles');

Dont forget to reload/restart apache server

Sign up to request clarification or add additional context in comments.

1 Comment

I completed the steps to your first suggestion and now it works perfectly, thank you :)
0

If you're using a database to hold the page details when its created:

You can actually just add a DateTime Column in your database and then when the page is created and you Insert whatever you need to add, make the Column a default TimeStamp and it should automatically add the time and date the page was created for you.

To then get the TimeStamp out of the database (MySQLi version):

$db = mysqli_connect('host', 'user', 'pass', 'dbname');
$result = $db->query("SELECT * FROM tbl_name");
// add an error check here
while($row = mysqli_fetch_array($result)){
   if($row['columnPageID'] == 'the page ID you are on'){
       $createdOn = $row['columnName'];
   }
}
$db->close();

This code will not work, its so you get an understanding.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.