How to Convert String Date to Date Format in PHP


In this tutorial we will learn that how to convert string date to date format in PHP.

Using below code we can convert string date to any date format like Y-m-d, m/d/Y, d-m-Y etc in php.


Here we will use date() function for convert string date into any date format in PHP.

We also take an example to convert date to string date in PHP.

 

Now follow below code to convert string date to date format in PHP.

 

1) Convert string date to date format in PHP

<?php

    $OurStringDate = "1564079400";

    date_default_timezone_set('Asia/Kolkata');

    $DateFormatFirst = date('m/d/Y'$OurStringDate);

    print_r($DateFormatFirst);

    echo "<br><br>";

    $DateFormatSecond = date('m-d-Y'$OurStringDate);

    print_r($DateFormatSecond);

    echo "<br><br>";

    $DateFormatThird = date('m/d/y'$OurStringDate);

    print_r($DateFormatThird);

?> 


Output

image

 

2) Convert date to string date and then convert to any date format in PHP

 <?php

 $OurStringDate = strtotime('17-11-2021')//converting date to string date

 

    date_default_timezone_set('Asia/Kolkata');

    $DateFormatFirst = date('m/d/Y'$OurStringDate);

    print_r($DateFormatFirst);

    echo "<br><br>";

    $DateFormatSecond = date('m-d-Y'$OurStringDate);

    print_r($DateFormatSecond);

    echo "<br><br>";

    $DateFormatThird = date('m/d/y'$OurStringDate);

    print_r($DateFormatThird);

?> 


Output

image


Related Topics