Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Changing datetime in computer affects CURRENT_TIMESTAMP in SQL

I have blog page in my website. Whenever I write a blog, I only save the content of the blog, and let the SQL handle the current timestamp.
So my code goes like this:
[CODE lang="sql" title="SQL code"]CREATE TABLE blogs(
blogID INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
content TEXT NOT NULL,
img VARCHAR(128),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);[/CODE]

However, when I changed the time in my computer 1 day ahead, the CURRENT_TIMESTAMP also saves 1 day ahead.
This made me think what would be the best way to get the current datetime that does not affect saving even if I change computer's datetime.
should I save the date in PHP code or is there something else to do or code in SQL to achieve this?
 
Not sure about this, so give it a test. I think you can force PHP to not use the computer's time by the command
date_default_timezone_set('YOUR TIME ZONE HERE');
A list of time zones can be found here: https://www.php.net/manual/en/timezones.php

Do this before any calls to time or date.
I actually did this before I change my database design with created_at using CURRENT_TIMESTAMP.
I guess I'm going back return it to my old code, which is manually saving the date to db.
 

Buy us a coffee!

Back
Top Bottom