How to convert an Oracle timestamp object into a php date? - php

The date data is like this
09-OCT-12 03.19.59.000000000 PM
and it is coming from an object:
$objectvar->datedata;
I've tried this
$date = new DateTime($objectvar->datedata);
echo $date->format('M d,Y');
it doesn't work, it doesn't display the correct data from the object;
I've also tried this
echo date('M d,Y',strtotime($objectvar->datedata));
it doesn't work too, like it doesn't display the correct data from the object;
what can I do to correctly convert the date?

i've found the correct answer
$date = DateTime::createFromFormat($objectvar->date);
$converteddate = $date->format('M d,Y');
echo $converteddate;

This answer should be here:
$dt = DateTime::createFromFormat("d#M#y H#i#s*A", $objectvar->datedata);

There is probably a native way of doing it, but maybe you can use this method instead in the meanwhile:
list($day,$month,$year) = sscanf($objectvar->datedata,"%d-%s%-%d");
now you ca use it to create your date format
echo date("M d, Y",strtotime("$year-$month-$day"));

Related

Format DateTime with PHP

My API outputs the DateTime in the following format:
2018-06-17T09:07:00Z
How do I display this in a more meaningful way, say, 17/06/2018.
I looked at the Manual: http://php.net/manual/en/datetime.formats.date.php however still wasn't able to find a way to achieve this.
$eventStart = "2018-06-17T09:07:00Z";
You can format it like the below code in PHP:
echo date('d/m/Y', strtotime('2018-06-17T09:07:00Z'));
Use Below code.
date('d/m/Y', strtotime('2018-06-17T09:07:00Z'));
Use Date Format :
$inputDate = "2018-06-17T09:07:00Z";
echo date('d/m/Y', strtotime($inputDate));
Convert the string in time and then format the date which you want
Date-Format option
$date = '2018-06-17T09:07:00Z';
echo date('d/m/Y', strtotime($date));
Format the date, but first convert the string to time.
echo date('d/m/Y', strtotime($inputDate));

Format mySQL timestamp by changing date-month and remove seconds using PHP function?

I echo $istoriko['timestamp'] from mySQL and I get 2014-04-24 00:10:29
How can I change it to
24/04/14 (or 2014) 00:10
Thank you
Try this, its simple
You can set any date format now in date functions
echo date("d/m/y",strtotime($istoriko['timestamp']));
Follow functions can help you:
$date = '2014-04-24 00:10:29';
$date = date('d/m/Y H:i',strtotime($date));
echo $date

timestamp to php from timestamp array

Hi guys i am really new to php and i am trying to convert the timestamp from an xml array but with no sucess , i read everything i found but still can find the way can you please help ?
I am using this code to decode an xml api output
$mysongs = simplexml_load_file('http://example.com/test/xml/');
$timestamp = $mysongs->item->timestamp;
$playtime = date("Y-d-m G-i-s",$timestamp);
If i echo $timestamp it works fine, $playtime doesn't...
Tried with :
echo gmdate("Y-m-d", $timestamp);
&
echo date('Y-m-d H:i:s', $wra);
&
echo '<timeplayed>' . date('Y-m-d G:i:s', $mysongs->item->timestamp) . '</timeplayed>';
Still no luck.. Time is not showing.. If i use this example
echo '<timeplayed>' . date('Y-m-d G:i:s', (1365532902)) . '</timeplayed>';
it works fine.. What am i doing wrong here ?
Update
Finally i found it.. it needed to cast the $timestamp as integer for the date to decode properly as euxneks sugested..
So right code should be
$timestamp = intVal ($mysongs->item->timestamp);
and then
$playtime = date("Y-d-m H-i-s",($timestamp));
& finally echo ($playtime); and it works fine...
Thanks everyone for your replys problem solved
It's likely that you need to cast your result as an integer (it's been a while since I've used simplexml but I'm pretty sure it doesn't automatically type the values received):
http://php.net/intval
Also, strtotime might work too :)
http://php.net/strtotime
You are definitely missing strtotime function, here is an example:
$str = '04/09/2013';
$date = date('Y-m-d H:i:s',strtotime($str));
echo $date;
This will output:
2013-04-09 00:00:00
Updated
Since strtotime is alread used, what about using:
$date = date('Y-m-d H:i:s', ($timestamp));

Format DATETIME from MYSQL database using PHP

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

php convert date value preferred to mysql date

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));

Categories