Welcome Guest, Not a member yet? Register   Sign In
Basic date formatting problem:
#1

[eluser]Sanity11[/eluser]
Hi all,

I have a question about formatting dates.

In my database I have two fields for DATE.
The date's in the database are formatted as follows: 2009-06-01. In the Netherlands it should be like this: 01-06-2009. To show it like that I have made the following:

Code:
<tr>
                                    <td>
                                        Start Datum:
                                    </td>
                                    <td>
                                        &lt;?php $time_start = human_to_unix($query[0]->start_date); echo mdate($datestring, $time_start); ?&gt;
                                        &lt;?=$query[0]->start_date?&gt;
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Eind Datum:
                                    </td>
                                    <td>
                                        &lt;?php $time_end = human_to_unix($query[0]->end_date); echo mdate($datestring, $time_end); ?&gt;
                                        &lt;?=$query[0]->end_date?&gt;
                                    </td>
                                </tr>

This however result's in:

Code:
<tr>
                                    <td>
                                        Start Datum:
                                    </td>

                                    <td>
                                        01-06-2009                                        2009-06-01                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Eind Datum:
                                    </td>
                                    <td>
                                        01-06-2009                                        2029-06-08                                    </td>

                                </tr>

As you can see the date processed is always: 01-06-2009 ...?
Can someone help me do this correctly?
#2

[eluser]jdfwarrior[/eluser]
There may be better ways to do it.. But you can always do something like use the explode command and break your date into pieces and then echo them how you want.

Edit: Guess I could elaborate more.

You could do.
Code:
$my_date = explode("-", "2009-06-01");
echo $my_date[2]."/".$my_date[1]."/".$my_date[0];

Or however you want it shown..
#3

[eluser]Sanity11[/eluser]
Thanks, that works.
I am still curious why my solution doens't do what I expect Smile.
#4

[eluser]Fabdrol[/eluser]
Hi Sanity11,

Wat je kan doen, om het makkelijker te maken bij toekomstige projecten is dit: sla je datums voortaan als time() op (unix time). Deze zijn veel makkelijker te verwerken. (bijv. zo: date("d.m.Y", $query[0]->end_date); )

Met je huidige opstelling is de oplossing van jdfwarrior prima.
Wil je het toch omzetten zonder explode, kun je het volgende doen:

Code:
$startdate = $query[0]->start_date;
$enddate = $query[0]->end_date;

$starttime = strtotime($startdate);
$endtime = strtotime($enddate);

// echo as day-month-year
echo "Begin: ".date("d-m-Y", $starttime);
echo "Einde: ".date("d-m-Y", $endtime);

-- english --
Hi Sanity11,

You could, for future projects, save the dates to the DB as unix time (time()Wink. Then it'll be much easier to process! like so: ( date("d.m.Y", $query[0]->end_date); ).

With your current database you should do fine with jdfwarrior's solution. If you insist on not using explode(); you could use the following code.

Code:
$startdate = $query[0]->start_date;
$enddate = $query[0]->end_date;

$starttime = strtotime($startdate);
$endtime = strtotime($enddate);

// echo as day-month-year
echo "Begin: ".date("d-m-Y", $starttime);
echo "Einde: ".date("d-m-Y", $endtime);
#5

[eluser]drewbee[/eluser]
As a quick note, I actually use DATETIME formats for all dates and what not... and you do have to do a little conversion. The easiest way I have found is to use:
Code:
$query->get_record();

echo date('m d Y h:i:s', strtotime($query->datetime));
#6

[eluser]xwero[/eluser]
the human_to_unix function doesn't work as it requires a date in following format : 2006-08-21 11:35 PM. the mysql_to_unix function will not work either if the value is only a a date.

As others mentioned the php functions are more flexible.
#7

[eluser]Sanity11[/eluser]
Thanks for the great reply's. Now I can continue with a better understanding of working with dates. Smile




Theme © iAndrew 2016 - Forum software by © MyBB