I have a variable($jdate_post), and I want convert it’s value to some variables(Ex: $year, $month, $day…) by explode function. but when I want to convert these variables(Ex: $year) to integer, it returns 0. Help me, please.

  $array = explode(' ', $jdate_post);
  list($year, $month, $day) = explode('-', $array[0]);
  list($hour, $min, $sec) = explode(':', $array[1]);
  echo intval($hour); 

Please let us know the format of your $jdate_post variable. Did you load it from a database field of a format of DATE or DATETIME? Or is if from a user input form. If from a form, is it a TIME-DATE PICKER input format or just a text field. Once we know the format, we can help you convert it to the correct version for displays.

Normally, you have two ways to process dates and times. In text version or in a DATETIME object format. Each are easy to handle, but, you must use different functions to deal with them.

Also, in your code, you show the format as text with year-month-day. If so, you do not need to explode them, just convert them to a DATETIME object and use the builtin functions for dates. Some of them are simple. Convert text to date object like this: $date_time = strtodate($jdate_post); Or something close to that. Then, you can just use echo year($date_time); to display the year, etc…

Here is a list of most of the common date functions. Again, it depends on your format of your data and how it was created. Date-Time Functions

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service