jst a small Query..!!
How to convert string to Date format.. So tht i can get result from database..!!!

$date = "mm/dd/yy";

i want in yy-mm-dd format...!!! which function is used for it.!!
waiting for positive reply..!!

Recommended Answers

All 13 Replies

Hi nish123,

You will need to use two function calls for this, strtotime and date. First you use strtotime to convert your string to a unix timestamp, like so:

$time = strtotime( $date );

Then you use this timestamp to calculate a date in whatever format you want. To get the format yy-mm-dd, you need the following formula string: 'y-m-d'. So the call to date would look like this:

$myDate = date( 'y-m-d', $time );

For more details on these two functions, please see the documentation at http://www.php.net/manual/en. The documentation on the date function includes the common format strings that you can use to format your date.

yes this is possible using php function like

$dob1=trim($_POST['txt_dob']);//$dob1='dd/mm/yyyy' format
			list($d, $m, $y) = explode('/', $dob1);
			$mk=mktime(0, 0, 0, $m, $d, $y);

if you can convert string date to date formate direct using strtotime() function this not give valid date
i have all ready faced this problem

i hope this code will helpfull to you

if you want to convert y-m-d formate to dd/mm/yy than you can use
this type of code

			 	$new_date=date('dS M, Y', strtotime($update_date));

if you did not do like this than blank date automatic convert into 1969 date


Member Avatar

Personally, I hate strtotime.
You could do this (less eloquent than dark's), but here goes:

$date = [something in US format: mm/dd/yy]

$newdate = substr($date,6,2) . "-" . substr($date,0,2) . "-" substr($date,3,2);

This assumes that the original date will always have 2 digits (including a leading zero for numbers less than 10) for each date element. I have to say a 'two digit year' is horrible - any way of changing it to 4 digits?

commented: yah really got it +1

Thanks to everybody...!!!
Problems is sloved..!! :)

you should marked issue as solved

I basicly want to take three inputs and make them into one date object.

Should I then combine them into one string first?

so would it be
$datestring= $year.$month.$day;


$time = strtotime( $datestring );

$myDate = date( 'y-m-d', $time ); ?

$query_att_cek = SELECT id,nosb_m,class_id,year,STR_TO_DATE(tarikh, '%d-%m-%Y'),attend,komen FROM attendance where 'tarikh between $date1 and $date31' and class_id = '$_SESSION[class_id]' group by tarikh;

help me please

Thanks it worked

I am having a user defined string like Tue 12 June, 09.30 AM will you please help me to convert it into 2012-06-12 09:30:00 in php.

In Advance thanks a ton.

i have my string as 19930804 format (YYYYMMDD) and i need to convert this into 1993-08-04 date format can any one help me pls

use date("d-m-Y",$date);

commented: its a solved thread, its a year old, the op won't be back to look, it won't make you look like you know what you're doing, not much point really -3

Well I think if you do these steps then it will be easy.

1: input field
<input type="date" name="date">

2: Retrieve date in php
$date = $_POST["date"];
$date = filter_input(INPUT_POST,"date");
$date = $_GET["date"];
$date = filter_input(INPUT_GET,"date");

3: Send it in query
$query = "INSERT INTO TABLENAME (DATE) VALUES ('".$date."');";

Be a part of the DaniWeb community

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