How would you convert the date-time coming from an SQL server into another format? The value coming from the SQL database is like 2013-09-23 12:20:30 and I want it to be displayed like Sept-23 12:30 pm.
I can send the date-time to server in this format by PHP, but then I will have a problem when I need to compare the date-time in SQL. The field in the database is set to the datetime type.
I've tried:
<?php
if(substr($postainarray['time'],8,2) == $CurrDate) {
$timetobeshown=substr($postainarray['time'],11,5);
} else {
$timetobeshown=substr($postainarray['time'],5,11);
}
?>
<?=$timetobeshown?>
$currDate is today's date.
$postinarray is the array after the MySQL query.
After this code I get the desired format but not in desired style. If the date matches I get something like 14:00, otherwise I get 09-24 18:09, and I want it to be like Sept-24 6:09 pm.
$a='2013-09-23 12:20:30';
echo date("M d g:i a",strtotime($a));
for date format manipulation, the reference page is: PHP:date Manual
there you can checkout all the options you might need, and for your case it will be:
echo date("M-d g:i a", certaintaime);
Try this,
<?php
$date_from_db='2013-09-23 12:20:30';
echo date("M-d g:i a",strtotime($date_from_db));
?>
Related
I am storing dates in my database like this: 2014-09-08. I want to retrieve them to my php script inverted: 08-09-2014. They are DATE.
How can I do this in my query? Or even in php if not possible.
Using MySQL, you can use DATE_FORMAT():
SELECT DATE_FORMAT(`date`, '%d-%m-%Y');
Using PHP, you can do this way:
echo date("d-m-Y", strtotime($row["date"]));
Fetch the date from the database and use date() function to format date. DEMO The demo outputs 08-09-2014.
<?php
$date = $row["date"]; //if value of date is 2014-09-08
echo date("d-m-Y", strtotime($date)); //outputs 08-09-2014
?>
In PHP, I'm saving a date record as string in order to make conditions like "more than", "less than"; but I would really like to know how to decode it. What I want to do is something like this:
The moment that record is being saved is 2014, 08/06 1:30 in 24-hours format, so my integer should be like 201408060130, of course for this I use date() function.
But when it comes to decoding it to show it back like 2014, 08/06 1:30 or another format like 08/06 2014, 1:30 I really get stuck thinking on any solution for this.
I thought it would be like:
$date = date('YdmHm'); //Saving as 201408060130
$this->saveToDatabase($save, $mytable);
$dateDecoded = $this->getFromDatabase($mytable, $id, $theDateInteger);
$result = Date::getFormat($dateDecoded, 'YdmHm'); //Decode date format
echo $result->date('Y d/m, H:m'); //Show 2014 08/06, 1:30
This is simple & works. Just use strtotime:
$test_value = '201408060130';
echo date('Y d/m, H:i', strtotime($test_value));
The output is:
2014 06/08, 01:30
PHP has a really useful method under the Date class for this called DateTime::createFromFormat()
Here is an example usage:
$datetime = DateTime::createFromFormat('YdmHm', (string)$theDateInteger);
Now this is a valid datetime object which you can save to any formatting in a string if desired. For example:
echo $datetime->date('Y d/m, H:m');
How to get current date in codeigniter in YY-mm-dd format. I wants to get current date in YY-mm-dd frmat and put this value into input text box
You can use the PHP date function.
date('Y-m-d');
Up to my knowledge, there is no separate date function in codeigniter.
EDIT :
But if you want date in this format 13-04-05 [ yy-mm-dd ], Try this
date('y-m-d');
For more date formats, check this link PHP Date Formats
Try to use this is a generic format of DateTime
echo date('Y-m-d H:i:s');
use php date function
echo date("Y-m-d");
will give you the result
What about:
$date = new \Datetime('now');
var_dump($date);
if you want the full date:
echo date('Y-m-d');
depends your date structure.
echo date('d-m-Y');
if you want year only, then echo date('Y'); is enough
Use php date() function, like this
echo Date('Y/m/d');
it give you the desired result!
So I have a field in my database called 'DateTime' and the following lines of code:
echo "Date/Time: ";
echo $row['DateTime'];
How do I format it so that instead of being like this:'2013-02-07 22:14:56', it will be like this: '07/02/13 - 22:14'
Thanks.
Alternatively you could use:
DateTime::createFromFormat('Y/m/d H:i:s',$row['DateTime']); this will give you a datetime object, which are quite nice to work with.
Another alternative would be to have MySQL format the DATETIME value as a string in the desired format, using the DATE_FORMAT function.
SELECT DATE_FORMAT(`DateTime`,'%d/%m/%y - %H:%i') AS `DateTime`
...
No change required to your PHP code except for the SQL text sent to the database server.
This approach can very efficient, and reduce the amount of code you need, if all you are doing with this string is displaying it. If you are doing any sort of manipulation on this value, then casting the string value returned from MySQL resultset into a datetime object is probably a better way to go.
A demonstration of the DATE_FORMAT function:
SELECT DATE_FORMAT('2013-02-07 22:14:56','%d/%m/%y - %H:%i') AS `DateTime`
DateTime
----------------
07/02/13 - 22:14
how to output date into Year textbox Month textbox Day textbox
$book_date = $myrow["Publication_Day"];
$book_year = Date("Y", strtotime($book_date));
$timestamp contains ur date & time in any format.....................
date('Y/m/d - H:i',strtotime($timeStamp));
echo date('d/m/y H:i', strtotime($row['DateTime']));
See date and strtotime for more detail on the functions from the docs
$mytime = strtotime('2013-06-07 22:14:56');
$newDate = date('m/d/y - G:i', $mytime);
echo $newDate;
Here's an alternative using DateTime. If you're working with timezones this code can be easily modified to handle that.
$datetime = new DateTime('2013-02-07 22:14:56');
echo $datetime->format('d/m/y H:i');
See it in action
I have this string I'm getting from the GUI: 09/22/2012 (mm/dd/yyyy).
I want to format it to:
date("Y-m-d"); //(the MySQL DATE format )
What should be the right PHP date function parameters to do so?
You can try
$date = DateTime::createFromFormat("m/d/Y", "09/22/2012");
echo $date->format("Y-m-d");
Output
2012-09-22
You can also do the following to get the MySQL date format.
echo date('Y-m-d', strtotime('09/22/2012'));
// Output: 2012-09-22
Hope that helps.