Skip Navigation

[Resolved] Timezone in a date function

This support ticket is created 2 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 6 replies, has 2 voices.

Last updated by Pat 2 years, 7 months ago.

Assisted by: Christian Cox.

Author
Posts
#2131485

Pat

Hello,

I'm coming back on the previews topic : https://toolset.com/forums/topic/condition-with-a-date-function/
The date is going well, but I still have an issue with the hours. I have 2 hours difference between the hour placed in the custom field and the output condition result.
My WordPress parameters are based on Paris timezone.
I have also tried to add some code in the function :

add_shortcode('post-today', 'today_shortcode');
function today_shortcode() {
date_default_timezone_set('Europe/Paris');
    $timestamp = time();
    return $timestamp;
}

I'm still using the same condition :

[wpv-conditional if="( date_debut_ins() lte [post-today] ) AND ( date_fin_ins() gt [post-today] )"] [wpv-woo-buy-options] [/wpv-conditional]

Did I miss something?
Regards
Pat

#2131713
Screen Shot 2021-08-02 at 2.46.34 PM.png

I would need to see the conditional debug information and all the raw field values to give you some advice. I looked at the other ticket and it looks like one of the shortcodes was not defined correctly. See the screenshot here. If you correct the fin shortcode name, then you should be able to output all the information with shortcodes so I can see the results, like this:

debut shortcode: [date_debut_ins][/date_debut_ins]<br />
debut field: [types field="date-debut-inscription-site" output="raw" item="18207"][/types]<br />
fin shortcode: [date_fin_ins][/date_fin_ins]<br />
fin field: [types field="date-fin-inscription-site" output="raw" item="18207"][/types]<br />
post_today: [post_today][/post_today]<br />
Conditional: [wpv-conditional if="( date_debut_ins() lte [post_today][/post_today] ) AND ( date_fin_ins() gt [post_today][/post_today] )" debug="true"]Both dates are in range[/wpv-conditional]

What do you see on the front-end of the site? Please copy + paste the results for me to review.

#2131767

Pat

Hi Christian,

I have modified your code as follow :

debut shortcode: [date-debut-ins]<br />
debut field: [types field="date-debut-inscription-site" output="raw" item="18207"][/types]<br />
fin shortcode: [date-fin-ins]<br />
fin field: [types field="date-fin-inscription-site" output="raw" item="18207"][/types]<br />
post_today: [post-today]<br />
Conditional: [wpv-conditional if="( date_debut_ins() lte [post-today] ) AND ( date_fin_ins() gt [post-today] )" debug="true"]Both dates are in range[/wpv-conditional]

I think there was a mismatch between funtion and shortcode name !
Here is the result :
debut shortcode: 1627910400
debut field: 1627910400
fin shortcode: 1655488800
fin field: 1655488800
post_today: 1627935594
Conditional: Both dates are in range

####################
wpv-conditional attributes
####################
Array
(
[if] => ( date_debut_ins() <= 1627935594 ) AND ( date_fin_ins() > 1627935594 )
[debug] => true
)

####################
Debug information
####################
--------------------
Original expression: ( date_debut_ins() <= 1627935594 ) AND ( date_fin_ins() > 1627935594 )
--------------------

In this case, I placed the date debut ins to August, 2nd 1PM, so it's normal to be in the range.
Now, I'm placing the current time (August, 2nd 10PM22), the condition is not right. If I'm using a 2 hours less time (ie : 8PM22), then the condition is right!
This sounds as a timezone issue (2 hours difference in the [post-today] shortcode !).

Let me know
Regards
Pat

#2133067

Now, I'm placing the current time (August, 2nd 10PM22), the condition is not right. If I'm using a 2 hours less time (ie : 8PM22), then the condition is right!
This sounds as a timezone issue (2 hours difference in the [post-today] shortcode !).

Okay so what do you want to do, add or subtract two hours to the output of the post-today shortcode?

60 seconds in a minute * 60 minutes in an hour * 2 hours = 7200 seconds in 2 hours

$timestamp = time() + 7200
or
$timestamp = time() - 7200.
That will adjust the timestamp by two hours. I'm not sure if you are saying you want to add or subtract.

#2133839

Pat

Hi Christian,

Thanks for your proposal. I'm OK with it but still have some concerns about the rootcause of this issue.
I have tried to add a line to define the right timezone in php :

date_default_timezone_set('Europe/Paris');

This does not change anything (even if I replace Europe/Paris with an Asian location !).
My concern here is that your solution could work in 90% of the time, but what would happen if for example, in the summer time change, there is a difference between France and other countries? It's possible that in this case, the 2 hours difference becames only 1 and so, the condition will not be exact
Any idea on this?
Regards
Pat

#2134033

Any idea on this?
Well if you do not want to use a hard-coded 7200 seconds, then you should calculate the actual timezone time difference. If you want to determine the timezone offset programmatically, that's what PHP's timezone calculation functions are for:
https://www.php.net/manual/en/datetime.getoffset.php

#2135715

Pat

Hi Christian,

I have applied your solution and everything is fine.
Thanks
Pat

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.