Convert String to Date
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..!!
nish123
Junior Poster in Training
83 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
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.
darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
hi
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);
$dob_disp1=strftime('%Y-%m-%d',$mk);
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
Thanks
Tulsa
Junior Poster in Training
77 posts since May 2009
Reputation Points: 13
Solved Threads: 15
hi
if you want to convert y-m-d formate to dd/mm/yy than you can use
this type of code
if($update_date!="")
{
$new_date=date('dS M, Y', strtotime($update_date));
}
if you did not do like this than blank date automatic convert into 1969 date
Thanks
Tulsa
Junior Poster in Training
77 posts since May 2009
Reputation Points: 13
Solved Threads: 15
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?
diafol
Rhod Gilbert Fan (ardav)
7,792 posts since Oct 2006
Reputation Points: 1,170
Solved Threads: 1,080
Thanks to everybody...!!!
.
.
Problems is sloved..!! :)
nish123
Junior Poster in Training
83 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0