So I have do another test where the [data-oggi] and the [data-oggi-totimestamp date_field='data-inizio'] shortcodes should show the same value, where the [data-oggi] is 23/08/2019 16:32 and date field is setted to 23/08/2019 16:32, I have that the [data-oggi] show 1566570740
and the [data-oggi-totimestamp date_field='data-inizio'] show 1566577920
What could be the differences? Could be the time zone?
I have solved using these custom codes:
1
// Add Shortcode
function wp_get_today() {
$date = date("H:i");
return strtotime('+2 hour',strtotime($date));
}
add_shortcode( 'data-oggi', 'wp_get_today' );
2
// Add Shortcode
function wp_date_totimestamp( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
'date_field' => '',
),
$atts
);
$date = do_shortcode("[types field='".$atts['date_field']."' style='text' format='Y/m/d H:i' index='0'][/types]");
return strtotime($date);
}
add_shortcode( 'data-oggi-totimestamp', 'wp_date_totimestamp' );
Shane
Supporter
Les langues:
Anglais (English )
Fuseau horaire:
America/Jamaica (GMT-05:00)
Hi Francesco,
I must apologize for the delayed response as i'm generally not available on the weekends.
However I see that you've made quite some progress.
Is this issue now resolved or do you still need some assistance from my site.
Please let me know.
Thanks,
Shane
Hi Shane,
is not completely solved beacuse I want to use the following string in the first shortcode
date_default_timezone_set('Europe/Rome');
instead of '+2 hour'.
But if I use that string, after I using the first shortcode in the post, all hours and minutes of the date time fields has setted to the 'Europe/Rome' Timezone, but in the main setting of the site the timezone was setted into 'Europe/Rome', and I don't know how to apply the timezone only in the first shorcode value...
Shane
Supporter
Les langues:
Anglais (English )
Fuseau horaire:
America/Jamaica (GMT-05:00)
Hi Francesco,
Are you able to get the timezone information of the server you are using ?
It should be in the phpinfo file.
To create a php info file please follow the instructions in the link below.
https://make.wordpress.org/support/handbook/appendix/creating-a-phpinfo-page/
Then you can send me a screenshot of the timezone information.
Thanks,
Shane
Shane
Supporter
Les langues:
Anglais (English )
Fuseau horaire:
America/Jamaica (GMT-05:00)
Hi Francesco,
So you're going to have to modify the timezone of your server.
To do this follow the instructions below.
lien caché
Then you can just set it to your timezone "Europe/Rome"
Finally check your phpinfo again to see if the timezone changes.
Thanks,
Shane
Hi Shane,
I have do that, in the phpinfo now the timezone is set into my timezone "Europe/Rome", but the [data-oggi] shortcode need also to be "return strtotime('+2 hour',strtotime($date));".
I don't know why!
Shane
Supporter
Les langues:
Anglais (English )
Fuseau horaire:
America/Jamaica (GMT-05:00)
Hi Francesco,
So you're saying that you still need to put the +2 hours to offset the time from UTC correct?
The strtotime($date); is still returning today's date value and time in UTC?
With this here $date = date("H:i");
You're only returning the Hours and Minutes in a 24 hour format. Not sure if this is exactly what you wanted since its doing a comparison
Could you let me know exactly what strtotime($date); returns ?
Thanks,
Shane
Hi Shane,
thanks to reply.
Yes, the date value and time still returning in UTC...
You can see the values that returns here using +2 hours lien caché
Shane
Supporter
Les langues:
Anglais (English )
Fuseau horaire:
America/Jamaica (GMT-05:00)
Hi Francesco,
This should be ok now. I've modified this to use the date object as i'm able to set the timezone of the date object.
The new code is.
// Add Shortcode
function wp_get_today() {
date_default_timezone_set('Europe/Rome');
$date = new DateTime();
return strtotime($date->format('Y/m/d H:i'));
}
add_shortcode( 'data-oggi', 'wp_get_today' );
Please let me know if this helps.
Thanks,
Shane
Hi Shane, it doesn't return any solution... sigh!
So, the [data-oggi] shortcode return the correct value, but all DateTime custom field values are returned +2 hours instead of as I have set in the post...
Shane
Supporter
Les langues:
Anglais (English )
Fuseau horaire:
America/Jamaica (GMT-05:00)
Hi Francesco,
I believe the problem is coming from this here
date_default_timezone_set('Europe/Rome');
I've made some more modifications to the shortcode so you should now be getting the correct information.
Please check now and let me know.
Thanks,
Shane
Now is perfect.
Thank you very very much!!!
Shane
Supporter
Les langues:
Anglais (English )
Fuseau horaire:
America/Jamaica (GMT-05:00)
Hi Francesco,
Happy I was able to resolve this one for you.
As a summary this is the final code.
// Add Shortcode
function wp_get_today() {
$userTimezone = new DateTimeZone('Europe/Rome');
$date = new DateTime('now',$userTimezone);
return strtotime($date->format('Y/m/d H:i'));
}
add_shortcode( 'data-oggi', 'wp_get_today' );
// Add Shortcode
function wp_date_totimestamp( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
'date_field' => '',
),
$atts
);
$date = do_shortcode("[types field='".$atts['date_field']."' style='text' format='Y-m-d H:i' index='0' ][/types]");
return strtotime($date);
}
add_shortcode( 'data-oggi-totimestamp', 'wp_date_totimestamp' );
You can mark this ticket as resolved when you're ready.
Thanks,
Shane