bowker Posted September 18, 2021 Share Posted September 18, 2021 Hi hope someone can help, I have a field called DateBooked which by default is NULL. As expected strtotime brings back 01/01/1970 I am using DateBooked as one of the fields on an email that is being sent automatically. I need an IF statement to check if DateBooked is NULL and if so return blank and if not return the date as d-m-y. The code below runs fine to insert the info in the email, I'm just not sure where to put the IF to check DateBooked is NULL or not. function getTrainingHtml($data) { $training = ''; foreach ($data as $value) { $training .= ' <tr style="line-height: 25px;"> <td width="15%" style="border-bottom: 1px solid #ddd;">'.$value['FirstName'].' '.$value['LastName'].'</td> <td width="40%" style="border-bottom: 1px solid #ddd;">'.$value['CourseName'].'</td> <td width="10%" style="border-bottom: 1px solid #ddd;">'.date('d-m-Y' , strtotime ($value['ExpiryDate'])).'</td> <td width="10%" style="border-bottom: 1px solid #ddd;">'.date('d-m-Y' , strtotime ($value['DateBooked'])).'</td> <td width="25%" style="border-bottom: 1px solid #ddd;">'.$value['Notes'].'</td> </tr>'; } return $training; } function executeQuery($query) Quote Link to comment https://forums.phpfreaks.com/topic/313767-strtotime-and-null-values/ Share on other sites More sharing options...
gw1500se Posted September 18, 2021 Share Posted September 18, 2021 Search engines are your friend. is_null Quote Link to comment https://forums.phpfreaks.com/topic/313767-strtotime-and-null-values/#findComment-1590079 Share on other sites More sharing options...
bowker Posted September 18, 2021 Author Share Posted September 18, 2021 I have tried a couple of different ways using isnull but I think the syntax is wrong or I am putting it in the wrong place. Quote Link to comment https://forums.phpfreaks.com/topic/313767-strtotime-and-null-values/#findComment-1590080 Share on other sites More sharing options...
ginerjm Posted September 18, 2021 Share Posted September 18, 2021 I don't see where you have started to use is_null..... Quote Link to comment https://forums.phpfreaks.com/topic/313767-strtotime-and-null-values/#findComment-1590081 Share on other sites More sharing options...
ginerjm Posted September 18, 2021 Share Posted September 18, 2021 How about this: foreach ($data as $value) { if (!is_null($values['DateBooked'])) $db = date('d-m-y',strtotime($values['DateBooked'])); else $db = ''; // Now use $db to output your date booked entry $training .= '...... Quote Link to comment https://forums.phpfreaks.com/topic/313767-strtotime-and-null-values/#findComment-1590083 Share on other sites More sharing options...
Barand Posted September 18, 2021 Share Posted September 18, 2021 Or you could do at the source when you query your data. SELECT Firstname , Lastname , Coursename , CASE WHEN DateBooked IS NULL THEN '' ELSE DATE_FORMAT(DateBooked, '%d-%m-%Y') END as DateBooked, , DATE_FORMAT(ExpiryDate, '%d-%m-%Y') as ExpirtyDate , Notes FROM .... Quote Link to comment https://forums.phpfreaks.com/topic/313767-strtotime-and-null-values/#findComment-1590084 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.