Question

i have three variables containing values shown below

                       $day=13
                       $month=2
                       $year=2013

i want to convert these three variable data into a date format in php and store in anoother variable Ho to do this??

Was it helpful?

Solution

Another way to handle this is to utilize DateTime class

$date = new DateTime($year.'-'.$month.'-'.$day);
echo $date->format('Y-m-d');

OTHER TIPS

PHP doesn't really have a date variable type, but it can handle time stamps; such a time stamp can be created with mktime():

$ts = mktime(0, 0, 0, $month, $day, $year);

This can then be used with date() to format it:

$formatted = date('Y-m-d', $ts);

Below code will do the trick for you. Pass the date format as you need to the date() function.

echo date("M-d-Y", mktime(0, 0, 0, $month, $day, $year));

O/P : Feb-13-2013

Use strtotime: http://php.net/manual/en/function.strtotime.php

$date_timestamp = strtotime($month . " " . $day . ", " . $year);

This returns a timestamp, which you can use to create a date:

$the_date = date("M d, Y", $date_timestamp);

However, if you just want a string of those dates:

$the_date = $month . " " . $day . ", " . $year;

You can use mktime() function

$ts = mktime(0, 0, 0,$month, $day, $year);

then pass $ts to date function

$formatted = date('Y-m', $ts);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top