0

To begin with, I currently have code that looks like this in the view:

    <input type="text" class="datepicker" name="start1"> <input type="text" class="datepicker" name="end1">

I am using this to get the start and end date for how long a person will be staying in a hotel. Currently, for example, if a person chooses May 16, 2014 as the start date and May 24, 2014 as the end date, it is being stored in the database as one row as such:

'Name of Person' 'Room Number' 'Start Date' 'End Date'

So for the previous example, it would be stored as:

John Doe 100 5/16/2014 5/24/2014

Instead of this however, I want each row to represent a date as such:

John Doe 100 5/16/2014

John Doe 100 5/17/2014

John Doe 100 5/18/2014

and so on and so forth until 5/24/2014.

My question is this, how can I parse the data so that I can store the data as such?

1 Answer 1

1

This is easily achieved using PHP's DateTime methods.

$from = new DateTime('2014-05-16');
$end = new DateTime('2014-05-24');
$end = $end->modify('+1 day'); 

$interval = new DateInterval('P1D');
$range = new DatePeriod($from, $interval, $end);

foreach ($range as $date) {
    $row = array(
        'name' => 'John Doe',
        'room' => 100,
        'date' => $date->format('n/d/Y')
    );
    print_r($row);
}

PS: Don't store month/day/year please. Use day/month/year. Confuses the heck out of people.

Edit: formatting nazi

Sign up to request clarification or add additional context in comments.

5 Comments

When are we gonna universally accept YYYYMMDD? Why is that not a universal timestamp?
Don't you mean YYYY-MM-DD HH:II:SS?
Kind of, but I don't see the need for the dashes. But yes, EU, US etc. all write dates differently.
“Don't store month/day/year please. Use day/month/year – don’t store as text at all, use an appropriate date type :-)
CBroe is right here. I meant to say "at least use day/month/year instead of that".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.