Welcome Guest, Not a member yet? Register   Sign In
Find out system date format
#1

[eluser]Unknown[/eluser]
Hi All,

I am new to codeIgniter as well as php. I am writing a small code to convert English date to local date(BS) in which I have to break down the date in three parts month, day and year. And check in which format the user have entered the date(dd/mm/yyy or mm/dd/yyyy or yyyy/dd/mm). For this, I have to retrieve the system date format so that I can break the date into month day and year on the basis of system date format. Any help will be appreciated. Thanks in advance

Shashi
#2

[eluser]Pascal Kriete[/eluser]
The 'system date' is just a unix timestamp (seconds since 01-01-1970 00:00:00). You can get it with the php time function. To format your time you use the date function.
#3

[eluser]err403_love[/eluser]
There's really no way you're going to be able to tell the difference between "DD/MM/YYYY" and "MM/DD/YYYY". For instance 04/05/2008 could be May 4th or Apr 5th and there is really no way you'll be able to decipher between the two unless you force all your users to input the date in the same way.

I think the best thing to do in this case is have a select menu with the month, then input boxes for the day and year, then you can combine the date into whatever format you need. This is really a must since you're relying on user input, and you can't always guess how they're inputting it.

An example could be:
Code:
<!-- INPUTS (in view) -->
<?=form_open('controller/function')?>
<select name="month">
    <option>January</option>
    &lt;!-- . more options .--&gt;
</select>
&lt;input name="day" type="text" /&gt;
&lt;input name="year" type="text" /&gt;

&lt;input type="submit" value="Submit" /&gt;
&lt;/form&gt;
......

&lt;?php

// Controller

if (isset($_POST['day'])
{
    // Will output 'May 15, 2008'
    $date = $_POST['month'].' '.$_POST['day'].', '.$_POST['year'];
    
    // Turn date into GMT Unix timestamp if necessary
    // Will output '1210824000' which is 5/15/2008 at midnight.
    $gmt_date = strtotime($date);
}

?&gt;

Hope this helps. I really don't see any easy way to find out a user's date writing preference.. If this is something that will be fairly involving on a user's end, as in a full-blown profile with a lot of options, your other option could be allow them to choose between how they want to see their dates.

For example:
Code:
// EXAMPLE query,  should be much more complex than this.
$query = $this->db->query("
        SELECT `date_format`
        FROM user_table
        WHERE `user_id` = ".$this->userid."
    ");
    
// I'm not sure if I've written the $query-> stuff correctly,  the Database class in CI confuses me. :)
if ($query->num_rows() == 1)
{
    $result = $query->result();
    
    if ($result->date_format == 'DD/MM/YYYY')
    {
        $datetime = date('d-m-Y', $dateline) // Changes the date into the format needed.
    }

    elseif ($result->date_format == 'MM/DD/YYYY')
    {
        $datetime = date('m-d-Y', $dateline) // Changes the date into the format needed.
    }
    
    elseif ($result->date_format == 'YYYY/MM/DD')
    {
        $datetime = date('Y-m-d', $dateline) // Changes the date into the format needed.
    }
    
    else
    {
        $datetime = date('m-d-Y', $dateline) // DEFAULT IF USER DOES NOT HAVE ONE FILLED OUT
    }
}

Edit: But then you'd also have to change the "$dateline" beforehand to the local time for that user. I don't show this in the above code.
#4

[eluser]Unknown[/eluser]
Thanks for the help. That's true we don't know in which format user will input the date. I think I should allow user to set their date format preference so that I can use the same format to process and display it.

Thank you very much.
Shashi




Theme © iAndrew 2016 - Forum software by © MyBB