1
I am finishing a data filtering system coming from the Database, using BETWEEN, my difficulty is in formatting the field 'date' to timestamp to do the filtering, follow my code:
$where = "date BETWEEN $start_timestamp AND $end_timestamp";
$this->db->where($where);
$plannings = $this->db->get_where('plannings', array('teacher_id' =>
$teacher_id, 'status' => '1'))->result_array();
foreach ($plannings as $row)
The variables $start_timestamp and $start_timestamp, are already in timestamp format, I tried this way and got the following result:
$where = "strtotime(date) BETWEEN $start_timestamp AND $end_timestamp";
Upshot:
I was able to solve it this way:
$dataInicial = date('Y-d-m', $start_timestamp);
$dataFinal = date('Y-d-m', $end_timestamp);
$where = "date BETWEEN '$dataInicial' AND '$dataFinal'";
$this->db->where($where);
$plannings = $this->db->get_where('plannings', array('teacher_id' =>
$teacher_id, 'status' => '1'))->result_array();
foreach ($plannings as $row)
Hence another problem arose, the date is saved as 'Y-d-m', so far all well, but if the day and the month is less than 10 (from 01 to 09), it is saved without zero!
Then it returns the correct data with data from other months or days, I believe I should create a replace, like this below, in the variables $stardate and $stardate;
$year . '-' . ($day < 10 ? str_replace('0', '', $day) : $day) . '-' . ($month < 10 ? str_replace('0', '', $month) : $month),
However, as mentioned above it does not work!
strtotime
will only work if the date format isyyyy-dd-mm
– rray
It’s in Y-d-m format.
– Pedro Paulo
swap strtotime for UNIX_TIMESTAMP $Where = "UNIX_TIMESTAMP(date) BETWEEN $start_timestamp AND $end_timestamp";
– Leo Nogueira
@Leo Nogueira, no error, but returned nothing too.
– Pedro Paulo