$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>date - Get system timestamp in wordpress|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

date - Get system timestamp in wordpress

matteradmin10PV0评论

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.

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.

Share Improve this question asked Feb 19, 2019 at 17:49 EpsiloncoolEpsiloncool 1473 silver badges14 bronze badges 9
  • 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 Commented Feb 20, 2019 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 Commented Feb 20, 2019 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 Commented Feb 20, 2019 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 Commented Feb 20, 2019 at 15:17
  • WordPress does not do anything like that. Where did you get that idea? – Jacob Peattie Commented Feb 20, 2019 at 17:16
 |  Show 4 more comments

1 Answer 1

Reset to default -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) );
?>

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far