This question already has answers here:
Convert a date format in PHP [duplicate]
(18 answers)
Closed 9 years ago.
I am using MySQL and php. When I get a date from MySQL it is in the format yyyy-MM-dd. Once I get this string, how can I convert it to format of example Jan 2 2013 in php?
I tried
date("M j Y", mysql_result($recordset, $i, 'date_started'));
using http://www.php.net/manual/en/function.date.php as a reference, but I get some weird date as the output.
The PHP date() method needs a timestamp, so convert your mysql date string to a timestamp first using strtotime():
date("M j Y", strtotime(mysql_result($recordset, $i, 'date_started')));
Or better yet, format your date in your mysql query directly using DATE_FORMAT. You don't really even need PHP to do this.
Using the DateTime wrapper gives you functionality that is worth having. To convert from mysql Format, simple follow this pattern.
$mysqlDate = '2014-01-01';
$myDate = new DateTime($mysqlDate);
echo $myDate->format('M j Y');
you could use date_format in mysql like this
DATE_FORMAT(NOW(),'%b %d %y') //%b is the short name of a month
Related
This question already has answers here:
Converting string to Date and DateTime
(13 answers)
Convert one date format into another in PHP
(17 answers)
Closed 3 years ago.
I'm trying to convert a string of numbers I am pulling from a db that looks like this '010219', representing January 2, 2019. I cannot find a way to convert this into 2019-01-02 using php, I just keep getting today's date from the functions I am trying.
Needs to be accomplished with no separators in original string.
There are a variety of ways to accomplish this, however the most concise is probably to use date_create_from_format.
An example is here:
$date = date_create_from_format('dmy', '010219');
This will output a Date as so:
echo date_format($date, 'Y-m-d');
Outputs: 2019-02-01
The date_create_from_format function accepts a parameter that defines the format of the date. In this case, the format is dmy which means:
d - Day of month as two-digit number (01-31)
m - Month of year as two-digit number (01-12)
y - Year as two-digit number
The documentation for date_create_from_format is here.
have you tried something like this?
<?php
$str="010219";
$date = DateTime::createFromFormat('dmy', $str);
echo $date->format('Y-m-d'); //2019-02-01
$time = strtotime('10/16/2003');
$newformat = date('Y-m-d',$time);
echo $newformat;
// 2003-10-16
Please see source: Converting string to Date and DateTime
split and concatenate with preg_replace
$newformat = preg_replace("/^(\d\d)(\d\d)(\d\d)$/","20$3-$1-$2","010219");
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I have a date that gets sent from a database which comes in a string format of '2018-01-01'. What I want to do is convert it into a string of month and year. E.G. 'January 2018'.
What would be the best way of doing so using PHP?
Try using the strtotime function to turn your string into a timestamp and the date function with the F Y format to get what you want:
$date = "2018-01-01";
$formatted = date("F Y", strtotime($date));
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I am taking the input in M-d-Y format from calender but I want to convert it into Y-m-d when it store in the database. What method can I use?
You can try this. strtotime is your friend.
echo date("Y-m-d", strtotime('Dec-02-2014'));
or mysql DATE_FORMAT function, but mysql stores dates in yyyy-mm-dd format.
I think it better to do it in Mysql itself. When we have so many function. Like below:
SELECT STR_TO_DATE('Jan-31-2014', '%b-%d-%Y');
You can use it for insert query like below:
Insert into tableName `date` = STR_TO_DATE(InputVal, '%b-%d-%Y');
For more info:http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date
Try this:
$date="05-25-2014";
print(date("y-m-d",strtotime($date)));
use the date() and strtotime() like:
$var = date('Y-m-d', strtotime($timestring))
Converting string to Date and DateTime
copy from: https://stackoverflow.com/a/6239010/2478144
Make not that there is a difference between using forward slash / and hyphen - in the strtotime function. to quote from php.net
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
php how to change date to this format [duplicate]
Closed 8 years ago.
I have this php script
date('dS F Y', strtotime($dateVariable))
The result is this:
01st September 2014
but I need the result as this: 01st Sep 2014
in other words, I need not the full name of the month. is that possible please?
date('dS M Y', strtotime($dateVariable))
Try as below
date('dS M Y', strtotime($dateVariable))
You can do it directly with the date function, using the 'M' mode.
http://php.net/manual/en/function.date.php
Alternatively, check out the function JDMonthName -- using mode 2 you can get the abbreviated month name.
http://php.net/manual/en/function.jdmonthname.php
Alternatively, check out the strftime function (much like printf) for formatting your date. The %b format gives the abbreviated month name.
http://php.net/manual/en/function.strftime.php
This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 9 years ago.
I tried converting
12-18-1997
to
18-12-1997
with this code
$new_date = date('d-m-Y', strtotime('12-18-1997'));
but it results in 18-12-1969
If I have to convert full date alongwith time then its converting fine but in the date I posted in question there is no time.
Use DateTime instead of strtotime():
$date = DateTime::createFromFormat( 'm-d-Y', '12-18-1997');
echo $date->format( 'd-m-Y');
You can see from this demo that it prints:
18-12-1997
strtotime is good, but it's not psychic or omniscient. you're feeding it a time string it's not able to parse properly:
php > var_dump(strtotime('12-18-1997'));
bool(false)
Since you simply assumed it's succeeding, you feed that false back to date(), where it's type-cast to an integer 0. However, your result is impossible, since int 0 as a date is Jan 1/1970. With timezone conversions, it'd be 31-12-1969 for you, NOT 18-12.
If you can't feed strtotime a format it understands, then use date_create_from_format and TELL it what what the format is:
$date = date_create_from_format('m-d-Y', '12-18-1997');
$text = date('d-m-Y', $date);