Question

I have an old app that let's users insert dates so everyone knows when they will be on vacation. Up until now, they had text field where they would enter text as they like ("1.1,5.1,21.1-25.1") or whatever they want as it is simple text field. This kind of input excludes any chance of filtering or search. I started playing with Yii not too long ago and this is first time i need to work with multiple dates and or date ranges.

What i need is advice on how to store those dates / date ranges into database? I know Yii has it's way to store single date (i have done it before), but i have no idea if it can work with date ranges and or multiple dates.

If any of you out there had similar problem i would apriciate your advice on how to store those dates and maybe extensions you used etc.

Of course i would like to make it user friendly with date pickers and search capabilities, but i'm taking it step by step. Once i have it stored correctly, searching and filtering wont be huge pain.

Was it helpful?

Solution

What i need is advice on how to store those dates / date ranges into database? 

Well that depends on how do you want to use the date range. It depends on what is the criteria for searching. Because If you dont need to search the dates regularly then there may be some dirty ways to accomplish this task.
But if you need to search it frequently then you should make explicit columns for starting and end dates in the database table.By making explicit tables you can search in date ranges easily. for example you can run an sql query like

SELECT * FROM yourtable WHERE startDate<="some date" AND endDate>="some date"

NOTE: You have to be careful about the format of date in your php code and format of date in database.
If you need to use date range just for calculation purposes then you can use simple php code to accomplish that.

$startDate = '2014-02-20';

$endDate = '2014-03-20';

$inputDate = '2014-02-28';
$start = strtotime($startDate);
  $end = strtotime($endDate);
  $input = strtotime($inputDate);
bool $isBetween=(($user >= $start) && ($user <= $end));

Yii way: Actually there is not yii way to work with date range through one window. Actually each framework provide basic independent access to all attributes.That does not mean you cant change the behavior. Yes you can, but you need to code more.
There are some extensions which you may find helpful in future
Adding a date range search for CGridView the easy way
How to filter CGridView with From Date and To Date datepicker

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top