0

I am trying to get the todays date , day and time .

Today is : 10th july 2018,tuesday and time is 3.23am

For time the below code works fine:

$wpplcurrenttime = current_time( 'g:i a', $gmt = 0 );
echo $wpplcurrenttime;

and out put is: 3.23 am which is correct

But strangely the date is not getting correct : the returned date is 9th july 2018,Monday

These are the codes i tried :

$dw = date('l');
echo $dw;
echo date(get_option('date_format'));
echo date('l jS F Y');
echo gmdate('w');

All of above codes gives a wrong date and day. I have double checked the WordPress general setting it is showing the date and time correctly.

Really appreciate the help Thanks

2

As mmm said, wp stores all dates&times in gmt, but will show times as per the setting timezone. So you can cgange the timezone without the history needing to be updated. I prefer to use the php datetime when working with time. Gives better control and flexibility, eg if you want to show events in different users timezones, let the system deal with daylight saving etc.

To Fetch the wp timezone and create tz object, then use

$tzs = get_option('timezone_string');
$tzobj = timezone_open($tzs); 

To create datetime object for 'now' in a particular timezone

$now = date_create('now',$tzobj );

to format dates & times, use any format accepted by date()

echo date_format($now, 'Y-m-d H:i:s');
  • i am having some troubles with get_option('timezone_string'); wordpress.stackexchange.com/questions/308238/… – Latheesh V M Villa Jul 11 '18 at 9:51
  • Possibly the site is using the old gmt offset (less good since it doest cope wiuth daylight saving). If no timezonestring found, use $gmt_offset = get_option ('gmt_offset') and deal with the offset – anmari Jul 12 '18 at 0:10

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.