Welcome Guest, Not a member yet? Register   Sign In
Format Date?
#1

[eluser]Jauhari[/eluser]
I have some question about MySQL date format and I want convert it to more better view format.

My MySQl format is like this 2008-02-04 and I want display my MySQL data date become
like this 11/03/2008 or Like this Monday, 10 January 2008 or something else. and When I make update the date format will be automatically back to 2008-02-04 format.

How to do that?

Please give me solutions.
#2

[eluser]xwero[/eluser]
check out the date helper. If you want more flexibility you can use the php functions date and strtotime.
#3

[eluser]Jauhari[/eluser]
Any example xwero?

Thanks
#4

[eluser]Jauhari[/eluser]
I was try with this code

Code:
$fdate = "Year: %Y Month: %m Day: %d";
echo mdate($fdate, $row->tanggal);

And got this result and error.

Code:
A PHP Error was encountered

Severity: Notice

Message: A non well formed numeric value encountered

Filename: helpers/date_helper.php

Line Number: 89
Year: 1970 Month: 01 Day: 01

What's wrong?
#5

[eluser]xwero[/eluser]
i think you have to do this
Code:
$fdate = "Year: %Y Month: %m Day: %d";
echo mdate($fdate, mysql_to_unix($row->tanggal));
the mdate function is an interface function for the php date function so you need to convert the mysql date to the unix date format.
#6

[eluser]Jauhari[/eluser]
I have do this
Code:
echo mdate($fdate, strtotime($row->tanggal));

And produce the some result, can anyone show me different between mysql_to_unix and strtotime?

Thanks
#7

[eluser]xwero[/eluser]
This is the mysql_to_unix function
Code:
function mysql_to_unix($time = '')
    {
        // We'll remove certain characters for backward compatibility
        // since the formatting changed with MySQL 4.1
        // YYYY-MM-DD HH:MM:SS
    
        $time = str_replace('-', '', $time);
        $time = str_replace(':', '', $time);
        $time = str_replace(' ', '', $time);
    
        // YYYYMMDDHHMMSS
        return  mktime(
                        substr($time, 8, 2),
                        substr($time, 10, 2),
                        substr($time, 12, 2),
                        substr($time, 4, 2),
                        substr($time, 6, 2),
                        substr($time, 0, 4)
                        );
    }
The strtotime function is faster because it's a native php function.
#8

[eluser]Jauhari[/eluser]
Hi xwero

Thanks you fast reply, so it's will be better use strtotime than mysql_to_unix?
#9

[eluser]mironcho[/eluser]
Hi Jauhari,
Other possible solution is to use MySQL DATE_FORMAT() function in your sql code:
Code:
SELECT DATE_FORMAT(your_date_field, 'Year: %Y Month: %m Day: %d') AS formated_date FROM your_table;

If you prefer to format it in php - probably strtotime will be faster than mysql_to_unix.




Theme © iAndrew 2016 - Forum software by © MyBB