Skip Navigation

[Resolved] I need to create a conditional logic using the today date and the date field

This support ticket is created 4 years, 8 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.

Our next available supporter will start replying to tickets in about 0.24 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 29 replies, has 2 voices.

Last updated by Shane 4 years, 8 months ago.

Assisted by: Shane.

Author
Posts
#1323097

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?

#1323249

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' );
#1324381

Shane
Supporter

Languages: English (English )

Timezone: 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

#1324383

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...

#1324407

Shane
Supporter

Languages: English (English )

Timezone: 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

#1324413
phpinfo  .png

I think it is this

#1324501

Shane
Supporter

Languages: English (English )

Timezone: 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.
hidden link

Then you can just set it to your timezone "Europe/Rome"

Finally check your phpinfo again to see if the timezone changes.

Thanks,
Shane

#1324513

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!

#1324593

Shane
Supporter

Languages: English (English )

Timezone: 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

#1324975

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 hidden link

#1325273

Shane
Supporter

Languages: English (English )

Timezone: 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

#1325341

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...

#1325531

Shane
Supporter

Languages: English (English )

Timezone: 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

#1325551

Now is perfect.
Thank you very very much!!!

#1325563

Shane
Supporter

Languages: English (English )

Timezone: 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

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