Hi, i want to change format of date .
This code i add in my register page....

if(!empty($_POST['dob'])&& !empty($_POST['mob'])&& !empty($_POST['yob'])) {
$yob = mysqli_real_escape_string($con,$_POST['yob']);
$mob = mysqli_real_escape_string($con,$_POST['mob']);
$dob= mysqli_real_escape_string($con,$_POST['dob']);
$date = mysqli_real_escape_string($con,"$yob-$mob-$mob");
$addtothedb="INSERT INTO login (Dateofbirth) VALUES ('". $date . "')";          
$result=mysqli_query($con,$addtothedb);

and when i get back data from db and date show me like this (yyyy-mm-dd).but i dont want to like this. i would like to have like this (dd-mm-yyyy)

Hi!

That's the format supported by the database, save it like this, then when your get the result set format as you prefer. You can do it directly at query level with the format_date() function:

or in PHP with the IntlDateFormatter class:

For this task I use a little class, that can can convert also to a more readable format and display the month name in the language you prefer. Here's the code:

<?php

    class Dtime extends IntlDateFormatter
    {
        private $date;

        public function __construct($datetime = '', $pattern = 'd MMMM yyyy HH:mm', $locale = 'it')
        {
            parent::__construct($locale, IntlDateFormatter::FULL, IntlDateFormatter::FULL);

            $this->date = new DateTime($datetime);
            parent::setPattern($pattern);
        }

        public function get()
        {
            return mb_convert_case(parent::format($this->date), MB_CASE_TITLE, "UTF-8");
        }
    }

By default the language is set to Italian, but this can changed, as the format, usage example:

$date = '2015-04-14 21:00';
$dt   = new Dtime($date, 'dd-MM-yyyy', 'it');
echo $dt->get();

Which returns 14-04-2015, the default setting:

$date = '2015-04-14 21:00';
$dt   = new Dtime($date);
echo $dt->get();

returns:

14 Aprile 2015 21:00

There are also other methods to format a date, you can use the DateTime class or the strtotime() function:

Bye!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.