Formatting SQL Server time(7) using PHP date() - php

In my SQL Server database I have a time field stored as time(7). I have been trying to print this in PHP using echo but I wouldn't work, so I used this code
"ReturnDatesAsStrings" =>1
and it returned
08:35:00.0000000
And for the past hour I've been trying to format it into 08:35am using the date() function but have had no luck. Any clues?

If you disable ReturnDatesAsStrings what you get is a DateTime object. Obviously, a object cannot be printed with a plain echo, but you have a proper date and all you have to do is to call the format() method on it. Using strings as intermediate format when PHP can do it for you is a bigger hassle.

echo date('H:ia', strtotime('08:35:00.0000000')); // 08:35am
or, better, use the DateTime class:
$date = new DateTime('08:35:00.0000000');
echo $date->format('H:ia'); // 08:35am

Try this:
date_format( strtotime($datetime_field_in_db), 'g:i A') );

Related

Converting MYSQL timestamp to ISO-8601 without timzone offset

I'm trying to use timeago ( http://timeago.yarp.com/ ) and have found solutions for converting timestamps from MYSQL using php to ISO-8601.
date('c',strtotime($TimeStamp));
This works fine except im getting the timezone offset at the end
2011-07-10T08:46:50-**05:00**
what I want is 2011-07-10T08:46:50Z
Does anyone have a solution or know why i'm getting the timezone offset?
You can do it directly in MySQL:
SELECT DATE_FORMAT(yourfield, '%Y-%m-%dT%H:%i:%s0Z')
There is a GET_FORMAT(datetime, 'iso') call as well, but that returns the format string for ISO 9075, which is not quite what you want. Since it doesn't do 8601 directly, you have to build the format string yourself.
The 'c' will return the entire date, including the timezone offset. You will have to build it manually using the other options. Try this:
$time_stamp = time();
echo date('o-m-N',$time_stamp)."T".date('H:i:s',$time_stamp)."Z";
http://php.net/manual/en/class.datetime.php#datetime.constants.types
const string ISO8601 = "Y-m-d\TH:i:sO";
You can use your own format:
date('Y-m-d\TH:i:s\Z',strtotime($TimeStamp));
Simple for Open Graph
date('Y-m-d\TH:i', strtotime('2015/09/24 08:46:50))
Another way:
substr(date('c',strtotime($Timestamp)),0,-6).'Z';

PHP Zend date format

I want to input a timestamp in below format to the database.
yyyy-mm-dd hh:mm:ss
How can I get in above format?
When I use
$date = new Zend_Date();
it returns month dd, yyyy hh:mm:ss PM
I also use a JavaScript calender to insert a selected date and it returns in dd-mm-yyyy format
Now, I want to convert these both format into yyyy-mm-dd hh:mm:ss so can be inserted in database. Because date format not matching the database field format the date is not inserted and only filled with *00-00-00 00:00:00*
Thanks for answer
Not sure if this will help you, but try using:
// to show both date and time,
$date->get('YYYY-MM-dd HH:mm:ss');
// or, to show date only
$date->get('YYYY-MM-dd')
Technically, #stefgosselin gave the correct answer for Zend_Date, but Zend_Date is completely overkill for just getting the current time in a common format. Zend_Date is incredibly slow and cumbersome to use compared to PHP's native date related extensions. If you don't need translation or localisation in your Zend_Date output (and you apparently dont), stay away from it.
Use PHP's native date function for that, e.g.
echo date('Y-m-d H:i:s');
or DateTime procedural API
echo date_format(date_create(), 'Y-m-d H:i:s');
or DateTime Object API
$dateTime = new DateTime;
echo $dateTime->format('Y-m-d H:i:s');
Don't do the common mistake of using each and every component Zend Frameworks offers just because it offers it. There is absolutely no need to do that and in fact, if you can use a native PHP extension to achieve the same result with less or comparable effort, you are better off with the native solution.
Also, if you are going to save a date in your database, did you use any of the DateTime related columns in your database? Assuming you are using MySql, you could use a Timestamp column or an ISO8601 Date column.
This is how i did it:
abstract class App_Model_ModelAbstract extends Zend_Db_Table_Abstract
{
const DATE_FORMAT = 'yyyy-MM-dd';
public static function formatDate($date, $format = App_Model_ModelAbstract::DATE_FORMAT)
{
if (!$date instanceof Zend_Date && Zend_Date::isDate($date)) {
$date = new Zend_Date($date);
}
if ($date instanceof Zend_Date) {
return $date->get($format);
}
return $date;
}
}
this way you don't need to be concerned with whether or not its actually an instance of zend date, you can pass in a string or anything else that is a date.
a simple way to use Zend Date is to make specific function in its business objects that allows to parameter this function the date format. You can find a good example to this address http://www.pylejeune.fr/framework/utiliser-les-date-avec-zend_date/
this is i did it :
Zend_Date::now->toString('dd-MM-yyyy HH:mm:ss')
output from this format is "24-03-2012 13:02:01"
and you can modified your date format
I've always use $date->__toString('YYYY-MM-dd HH-mm-ss'); method in the past but today didn't work. I was getting the default output of 'Nov 1, 2013 12:19:23 PM'
So today I used $date->get('YYYY-MM-dd HH-mm-ss'); as mentioned above. Seems to have solved my problem.
You can find more information on this on output formats here: http://framework.zend.com/manual/1.12/en/zend.date.constants.html

PHP Datetime not working properly

I am retrieving the datetime data from mysql the retrieved data from a single row is.
2011-04-11 19:31:30
I wanted to reformat the datetime in d-m-Y H:i:s for that I am using date_format() the below code works just fine.
$date = new DateTime($users['registerDate']);
echo $date->format('d-m-Y H:i:s');
However, I don't want to go object oriented way just for reformatting because I will be using the code within the foreach loop and that means I would have to initialize the DateTime class again and again.
I tried doing this the Procedural way using the following code.
$date = $users['registerDate'];
echo date_format($date, 'Y-m-d H:i:s');
the above code does not work for me and gives back the following error.
Warning: date_format() expects parameter 1 to be DateTime, string given in /Applications/MAMP/htdocs/kokaris/administrator/resources/library/models/users/users.php on line 21
What could be possibly wrong?
The given solution works perfectly fine for the procedural way.
echo date('m-d-Y',strtotime($users['registerDate']));
However I would like to know which will be the best feasible solution the above procedural way or the OOP way.
$date = new DateTime($users['registerDate']);
echo $date->format('d-m-Y H:i:s');
Considering I will be using the code within a foreach loop and it may loop for over a hundred times.
You do not need "date_format()":
echo date('d-m-Y H:i:s', strtotime('2011-04-11 19:31:30'));
//results: 11-04-2011 19:31:30
You're trying to use a function/object that is part of the DateTime class without creating a reference to the DateTime class.
For procedural formatting take a look at date()
What you are seeking for is this:
date('d-m-Y H:i:s', strtotime('2011-04-11 19:31:30'));
Have a look at the php-manual. However, using the methods you yourself proposed is pretty fine, since the DateTime-object maps to some functions written in C.
Also PHP Datetime is working properly, since date_format is just an alias of Date::format, which does exactly require what you don’t want to pass in (a DateTime-object).
Honestly, we’re talking about PHP...

UTC datetime conversion

The values that I received from my device are: 090211 = ddmmyy and 062123 = hhmmss in UTC.
But I found that the time is always 8 hours later if compared to the time that I need. It is because the time for Malaysia is +8:00. First I would like to add 8 hour, and finally I would like to store this kind of date format into my MySQL database as "2011-02-09 06:21:23". How would I convert these values?
To convert in PHP to a datetime, you will need the function DateTime::createFromFormat(); This function will return a DateTime.
Using this function you can also pass the timezone as a parameter.
Example:
$date = DateTime::createFromFormat( 'dmy Gms', '090211 062123', new DateTimeZone("Europe/Amsterdam") );
You can create a output a following:
echo $date->format('Y-m-d H:i:s');
or UNIX timestamp for the MySQL:
echo $date->format('U');
Hope this helps!
PHP has both localtime and gettimeofday functions, are you by chance using the wrong one (or misinterpreting its results)?

CodeIgniter: format mysql DATETIME field dd/mm/yy hh:mm

Hi pretty much what it says on the tin.
I have a datetime mysql field I want to output in the format dd/mm/yyyy hh:mm like 07/01/2011 22:16.
I've tried:
<?php
$datestring = '%d/%m/%Y %h:%i';
echo mdate($datestring,$row->created);
?>
But I'm getting an error:
Message: A non well formed numeric value encountered
Any help most appreciated!
Cheers,
Billy
Try:
echo date ("d/m/Y h:ia",strtotime($row->created));
The second parameter of the mdate() function still needs to be an integer timestamp, just like the native PHP date() function. Try using the strtodate() function which accepts a string as a parameter (including the MySQL date format) and returns an integer. This can be done like this:
$datestring = '%d/%m/%Y %h:%i';
echo mdate($datestring, strtodate($row->created));
The only difference between mdate() and date() is, as the CodeIgniter docs say:
This function is identical to PHPs date() function, except that it lets you use MySQL style date codes, where each code letter is preceded with a percent sign: %Y %m %d etc.
The benefit of doing dates this way is that you don't have to worry about escaping any characters that are not date codes, as you would normally have to do with the date() function.
Got this to work using treeface's solution, with one minor change:
$datestring = '%d/%m/%Y %h:%i';
echo mdate($datestring, strtoDATE($row->created));
//strtoDATE didn't work but strtoTIME did
Had me scratching my head for hours, but now it works, I'm able to keep using CI helper for all date functions.
HTH
I'm using:
mdate(date_string,mysql_to_unix($row->created))
That should work.

Categories