convert one date format to another - php

I need help to convert a formatted date format as stated below:
How can I convert 2010-02-06 14:44:43 to dd/mm/yyyy
Thanks

Great place to start for this type of general information, php docs, specifically related to your date format question: http://us3.php.net/manual/en/function.date.php
-- Edit --
Answer with substr() forced me to do the work... using substr() to format a date is not the best option as PHP has built-in functions for that.
var_dump(date('d/m/Y', strtotime('2010-02-06 14:44:43')));

$date = new DateTime('2010-02-06 14:44:43');
echo $date->format('d/m/Y');
Output:-
06/02/2010
See the manual

Try this:
<?php
$time = strtotime('2010-02-06 14:44:43');
echo date('d/m/Y',$time);
?>

Related

want to convert my date formate 21/10/60 to 21-10-1960

I want to change date format from "21/10/60" to 21-10-1960 in PHP
i am using this code:-
$date = DateTime::createFromFormat('d/m/y', '21/10/60');
echo $date->format('Y-m-d');
and I am getting output:-
2060-10-21
but I want in this:-
1960-10-21
How can i Do this.
Since you mentioned repeatedly in your comments, like this one: minimum age is 16 years old, it will always be 1900's. You simply need
list($d,$m,$y)=explode("/","21/10/60");
echo "$m-$d-19$y"; // m-d-Y
echo "19$y-$m-$d"; // Y-m-d
No date functions or complicated stuff is required. Its a simple mapping job. As long as your actual date is valid and conforms to the format you mentioned, this output will be valid as well.
Fiddle

date format in MySQL table

I have the following two names in MySQL table:
message is type text
date is type datetime
<?php
while($row=mysql_fetch_array($result)){
?>
<tr>
<td><div><a><? echo $row['message'] ?></a></div></td>
<td><div><a><? echo $row['date']; ?></a></div></td>
</tr>
<?php
}
?>
The output of the above table shows date in this format 2012-08-05 17:43:57
How to print date in this format 2012-08-05 (without time)?
I cannot change type datetime in MySQl (it’s required in different pages in different formats).
I’ve tried the following, but it doesn’t help (no printing at all)
<td><div><a><? echo $rows['date'](“Y-m-d”); ?></a></div></td>
p.s. other combinations of date format give me syntax error.
Any suggestion would be much appreciated,
You want
<?php echo date('Y-m-d', strtotime($row['date'])); ?>
strtotime converts your datestring into a unix-timestamp
then, the date function formats it properly according to the string.
You want the date() to change the format.
There is something called TO_CHAR method in MS-SQL and Oracle in which you can pass the format you want your date in, so while fetching the data you can change it
You should use format function
echo new DateTime($row['date'])->format('Y-m-d');
UPDATED thanks to #Erty
http://php.net/manual/en/datetime.format.php
<td><div><a><? echo new DateTime($row['date'])->format('Y-m-d'); ; ?></a></div></td>
every one else is suggesting you do it with php, i always do this with mySQL's own date function:
SELECT ... DATE_FORMAT(Date_Field ,'%Y-%e-%c') ...
DATE_FORMAT
You first need to convert it to a date:
$tmpdate = strtotime($row['date']);
$date = date('Y-M-d', $tmpdate);
echo $date;
Of course you may short it down, as others suggested, but i prefer "the long way" to improve readability and help you get up to speed faster when you go back and review old code in the future. :-)

PHP : Function to convert the date format [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert one date format into another in PHP
I want to convert the date format from a particular format to some other.
For example,
Convert the date from ( 2011-06-21 or 2011/06/21 or 06/21/2011 ) to 21062011.
Is there any function to do this job.
Thanks in advance.
var_dump(
date('dmY', strtotime('2011-06-21')),
date('dmY', strtotime('2011/06/21')),
date('dmY', strtotime('06/21/2011'))
);
You should use the DateTime class.
$date = new DateTime('2011-06-21');
echo $date->format('dmY');
It can be used procedurally, if you wish.
var_dump(
date_format(date_create('2011-06-21'), 'dmY'),
date_format(date_create('2011/06/21'), 'dmY'),
date_format(date_create('06/21/2011'), 'dmY')
);
Hi you should be able to to use date combined with strtotime like so
$date; //the date that needs to be formatted
<?php date('dmY', strtotime($date)); ?>
So inside the date() function you simply format the date however you want the original date to be
php strtotime
Hope that helps
Unfortunately dates are very locale specific. strtotime() does not observe all of the niceties of the locale (and date_parse is a simple wrapper around strtotime). e.g. today is 21/06/2011 in the UK, and 06/21/2011 in the US.
A more robust solution is to use the DateTime class and its createFromFormat method.
However, IME, unless you are sourcing the input data from a consistent machine generated source, a better solution is to use a tool which facilitates input in a consistent format

PHP Date function

I meet a trouble with php data function.
echo strtotime("31/05/2011");
It prints empty.
What's problem with this?
Thanks.
DD/MM/YYYY Is not a valid date format (The manual outlines it must follow one of the supported date and time formats.) So, instead, it should be MM/DD/YYYY:
echo strtotime("05/31/2011");
Or, I guess as others have posted, the european (ISO8601 Notations) version uses hyphens:
echo strtotime("31-05-2011");
http://php.net/manual/en/function.strtotime.php
Use dashes instead of forward slashes.
echo strtotime("31-05-2011"); // outputs 1306821600
For European formatted dates (DD-MM-YYYY) use dashes not slashes:
echo strtotime('31-05-2011');
echo strtotime("2011-05-31");
How about using php's DateTime functions?
DateTime::createFromFormat('d/m/Y', '31/05/2011');

formatting a date output

I've got an arry with dates.
print_r ($date[$i]);
will output sth. like:
2011-06-16
Is it possible to create an output like: 16.6.2011 ?
How would I do that?
have you got a reference?
<?
echo date('d.m.Y', strtotime( $date[$i] ) );
?>
you should find that this page has all the answers for you
http://php.net/manual/en/function.date.php
You can use the date function to print it, using the strtotime function to first convert it to a Unix timestamp. Or you can use the DateTime functions like:
date_create($date[$i])->format('d.n.Y');
If you don't want leading 0's on your day, then you should use:
date_create($date[$i])->format('j.n.Y');
use the php date() function
you can see a documentation about it here
http://php.net/manual/en/function.date.php
You don't really have a date, but a string. Either manipulate the string with the string manipulation functions, or parse and re-format the date string with the date functions.
date('d.n.Y', strtotime($date[$i]));
There is a reference when you search in Google for "date" ;)

Categories