0

My system timezone is UTC+1, MySQL is using this timezone and it's ok. Now I want to get the same timestamps in WordPress. As you know, WordPress modifies the current timezone so time() returns UTC-related time. There is a possibility to use current_time('timestamp') to get either UTC timestamp or Wordpress-timezone timestamp. But my WordPress timezone is different from the system one.

So the question is: how can I get SYSTEM timestamp inside the WordPress environment? I guess they should leave a core function for that since I can not use time() anymore.

Of course, there is a stupid solution like (time() + 3600), but I don't like it.

  • What do you mean "My system timezone is UTC+1"? How have you configured this? By default PHP works in UTC, unless you've set it to something else in php.ini. But if you did set the timezone with php.ini then date() would display the time in that timezone. So I'm not sure what you're trying to do exactly. – Jacob Peattie Feb 20 at 10:58
  • Also, a "timestamp" is timezone independent. It's always "measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)." So it doesn't make much sense to talk about the system timestamp, since that is just time(). What do you intend to do with this timestamp? – Jacob Peattie Feb 20 at 11:26
  • @JacobPeattie UTC+1 is a timezone of my server (not PHP, not apache, I mean Ubuntu's system time). It does matter, because it's a default MySQL timezone too (system's timezone by default). What I need is to get the same timezone in the Wordpress. – Epsiloncool Feb 20 at 14:59
  • The problem with wordpress is the next: Wordpress resets timezone for PHP, so time() will always return UTC+0 datestamp! I am looking for a WP core function to get the system timestamp. – Epsiloncool Feb 20 at 15:17
  • WordPress does not do anything like that. Where did you get that idea? – Jacob Peattie Feb 20 at 17:16
-1

You can use PHP's date() function here, but usually first you'll need to run date_default_timezone_set. UTC+1 looks like Central Europe time from what I can tell, so here's what I'd run:

<?php
date_default_timezone_set('Europe/Berlin');
echo date( 'D, d M Y H:i:s', now() );
?>

That will print out the current time from your server.

If you're looking to print out a timestamp from a post, you can use get_post_time and print replace the now() function. Looks like this:

<?php
date_default_timezone_set('Europe/Berlin');
echo date( 'D, d M Y H:i:s', get_post_time('U', true) );
?>
  • Nah, I can't call date_default_timezone_set(). First, because I virtually don't know "system time" timezone. And second, I don't want to ruin Wordpress core logic, which is intended time()'s timezone is set to UTC+0. – Epsiloncool Feb 19 at 22:12
  • 1
    Also there is no "now()" function in PHP. – Epsiloncool Feb 19 at 22:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.