0

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)

2
Contributors
1
Reply
23
Views
2 Years
Discussion Span
Last Post by cereal
1

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!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.