Greetings. I need to translate the variable from string to date. Below is the code itself ...

$arFilter = array("ID" => 16104); $arSelect = array('NAME', 'PROPERTY_PAR_ID', 'PROPERTY_P_DATE', 'PROPERTY_P_NUMBER', 'PROPERTY_P_GIVER', 'PROPERTY_PAR_ID', 'PROPERTY_STATUS', 'ID', 'DATE_ACTIVE_FROM', 'DATE_ACTIVE_TO', 'DATE_CREATE', 'PROPERTY_DATE_IN', 'PROPERTY_DATE_OUT', 'PROPERTY_TYPE', 'PROPERTY_AUTHOR', 'PROPERTY_RESP_USER', 'PROPERTY_P_IS_ONETIME'); $res = CIBlockElement::GetList(Array(), $arFilter, false, false, $arSelect); $res = $res->Fetch(); $canget = true; if($res['DATE_ACTIVE_TO'] < date('dmY H:i')) { $canget = false; }else{}; echo gettype(date($res['DATE_ACTIVE_TO'])); echo "\n"; echo date('dmY H:i'); 

I get the following output:

string 11/14/2017 11:08

Please tell me what you need to write? I tried the option from the article How to convert a string to a php date? Unfortunately on my version, he did not work for some reason ....

  • echo $res['DATE_ACTIVE_TO'] - splash58

1 answer 1

To convert a string to a timestamp, you can use the strtotime function, and the time function returns the current timestamp.

Most likely, the comparison you need to do this way

 if(strtotime($res['DATE_ACTIVE_TO']) < time()) { 

The date function is needed to convert a timestamp to a character representation.

  • Yes, you were right. Reduction not to date, but to time. - Dmitry Goncharov
  • In my example, the page for passes is participating, and DATE_ACTIVE_TO is the date and time by which this pass is active. But there are cases when, let's say, booked for 11 am and came to 11:01. Transformed your structure like this: if (strtotime ("+ 3 hours", strtotime ($ res ['DATE_ACTIVE_TO'])) <time ()) {However, you must then limit the end of the day for DATE_ACTIVE_TO ... do not tell me how to set the end of the day? - Dmitry Goncharov
  • strtotime('midnight') - midnight - splash58
  • strtomite(date('Ymd') . ' 18:00') - respectively 18 hours today - splash58
  • Thank you for your help! - Dmitry Goncharov