I am fetching data from a database and outputting it as an XML file
I am using following format to fetch it
using for loop
$arrayName['fieldName'] which in my case is $row[publication_date]
In the database its in the format of mm/dd/yyyy ..but i just want yyyy to be outputted and I am trying the following code ::
$k = "SELECT EXTRACT(YEAR FROM'$row[publication_date]')";
if($k!=NULL){
$xml_output .="\t\t\t\t<year>" . $k . "</year>\n";
The result is
<year>SELECT EXTRACT(YEAR FROM'1/1/1995')</year>
But instead i want it to be just
<year>1995</year>
What i am doing wrong here?? plzz help me out..
Any questions plzz comment..:)
You can't just run SQL without executing a query in a database: you need to use PHP's functions.
$timestamp = strtotime($row['publication_date']); // convert date string to unix timestamp
$year = date('Y', $timestamp); // extract only the year
Store $year in your XML.
Reference:
http://php.net/strtotime
http://php.net/date
I would just pull the date normally without trying to format it on the database server and format it using PHP.
$date = // code here to gather the date
$date = date('Y', strtotime($date));
echo $date; // should return the 4 digit year
http://us.php.net/manual/en/function.date.php
Well, there's a few potential issues, but the first is that you need to actually use your database connection to run the EXTRACT command.
Alternately, with less overhead, you could use a string slice or regex to pull out the date.
How about:
$xml_output .="\t\t\t\t<year>" . preg_replace('#.*/#', '', $row[publication_date]) . "</year>\n";
Related
I have a large excel sheet which I am inserting data by bulk upload.
My problem is date:
date format is like 09/11/2017. I need to insert this data into table.
Any one can suggest me how to do this by conveting in mysql date format?
Use strtotime() on your first date then date('Y-m-d') to convert it back:
$dateFromExcel = strtotime('09/11/2017');
$newDateToInsert = date('Y-m-d',$dateFromExcel);
echo $newDateToInsert;
// 2017-11-09
if you are using PHP 5.2 or higher
use
$ymd = DateTime::createFromFormat('d/m/Y', '09/11/2017')->format('Y-m-d');
you need to get row first and then use like this.
This is a function to convert date from DD-MM-YYYY to YYYY-MM-DD :
function con2mysql($date) {
$date = explode("-",$date);
if ($date[0]<=9) { $date[0]="0".$date[0]; }
if ($date[1]<=9) { $date[1]="0".$date[1]; }
$date = array($date[2], $date[1], $date[0]);
return $n_date=implode("-", $date);
}
If you are using phpExcel library to uplod exccel file than you can use following line.
It works perfectly for me.
PHPExcel_Style_NumberFormat::toFormattedString($exceldate, 'YYYY-MM-DD');
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 get data from oracle database and date value kept on 27-MAY-09. I need to insert this value to mysql database via PHP. I need to convert date format as 2009-05-27.
Any one know about it please let me know correct php statement for do this.
use date() function
$date = '27-MAY-09';
$newData = date('Y-m-d', strtotime($date));
php fiddle
$date = DateTime::createFromFormat('j-M-y', $inputDate);
$newDate = $date->format('Y-m-d');
PHP 5.3 not earlier.
try this
$date1 = "27-MAY-09";
$data2 = date("Y-m-d",strtotime($date1));
i am trying to retrieve date from Mysql db to my html form in order to edit the records, now the way i used to insert the date into the database was getting Year - Month - Day each from a list and then unite them in one variable like this :
$dob = $_POST['Year'] . '-' . $_POST['Month'] . '-' .$_POST['Day'];
this will insert the value of $dob like this format 0000-00-00 in my table
the problem is that how i will retrieve this variable and split it each element to its specific list
what i tried seems useless and wrong which is this code
$row['userDateOfBirth'] = $year . '-' . $month . '-' . $day ;
then put each variable to (example:year)
<option value="1920" selected="selected"><? echo $year ; ?></option>
this did not work , how can i do this ?
Assuming you've got an actual date/datetime field, you can do the splitting inside MySQL:
SELECT YEAR(datefield), MONTH(datefield), DAY(datefield), etc...
which gives you three separate fields containing the individual date components. You could also do things like explode('-', $datestr) in PHP to decompose 'yyyy-mm-dd' into individual yyyy, mm, and dd chunks.
Lots of options, it's up to you to pick which one is easiest/best for your particular problem.
You can handle that on the client (php) side.
$year = date('Y', strtotime( $row['userDateOfBirth'] ) );
if you have an invalid date, $year will have 1970.
You could use
$row['userDateOfBirth'] = date("Y-m-d", strtotime($mysqlResultRow['dateField']));
Where basically you're telling the date() function to return a formatted string of the time passed to it, in this case created via strtotime().
Date() reference. The above example returns the date in "2000-01-01", see the reference for selecting the appropriate format for your project.
I'm hoping this will be a piece of pie for someone! String output is currently 12:00am for everything.
The following code from MySQL with format HH:MM:SS (hours_open, hours_closed)
$get_hours_sql = "SELECT * FROM client_hours ORDER BY day";
$get_hours_res = mysqli_query($dbConnect, $get_hours_sql) or die(mysqli_error($dbConnect));
// Establish the output variable
$hoursList = '<div class="right_bar">';
while ($productList = mysqli_fetch_array($get_hours_res)) {
$id_hours = $productList['id_hours'];
$day = $productList['product_name'];
$open = $productList['hours_open'];
$close = $productList['hours_close'];
$hoursList .= ''.date("g:ia", $open).' - '.date("g:ia", $close).'<br/>';
}
$hoursList .= '</div>';
echo $hoursList;
Output is currently
12:00am - 12:00am
looped.
I want to get the output to
11:00am - 11:00pm
which would represent the database entries.
Thanks!
I always find PHP <-> MySql date handling fiddly (got better with 5.3 though).
My guess is that the mysql query returns the date as a string and date() is expecting a time stamp.
Often, I just get mysql to format the date as a string for me and return it as an additional field, like so:
$get_hours_sql = "SELECT *,date_format(hours_open,'%h:%i %p') as hours_open_formatted, date_format(hours_close,'%h:%i %p') as hours_close_formatted FROM client_hours ORDER BY day";
then just use the formatted fields:
$hoursList .= ''.$productList['hours_open_formatted'].' - '.$productList['hours_close_formatted'].'<br/>';
Data accepts as it's second parameter a Unix timestamp, so what you're trying to do simply won't work. You could use either mysql's TIME_TO_SEC function, or php's mktime to convert the time string to a Unix timestamp.
Example:
$openHours = explode(':',$productList['hours_open']);
$timestamp = mktime($openHours[0],$openHours[1]);
$yourDate = date("g:ia",$timestamp);
Edit: I think you should try Ben's answer, I think it's a better solution than mine.