I have a column of type date (only date) in mysql. However, when I am using the following:
$Answer->dateCreated=date('d-m-y');
I'm getting an error
A non well formed numeric value encountered
Any idea??
MySQL's date format is yyyy-mm-dd, which in PHP would be date('Y-m-d'). Your format string is reversed and using 2 digit years instead of 4 - Y2k's old news by now... don't use 2 digit years anymore.
I just did this and it is working:
$Answer->dateCreated = strtotime("now");
Try:
define('MYSQL_DATE_FORMAT', 'Y-m-d H:i:s');
$Answer->dateCreated = date(MYSQL_DATE_FORMAT);
I understand you're not wishing to save the time, don't worry MySQL will ignore the time for these columns.
Try using $Answer->dateCreated=date('Y-m-d');
Related
In My SQL Database I have a Timestamp Column with values like this one representing the Date of the last edit:
2015-01-17 08:55:34.000000
I want to compare the Date with the current date and when is the same day I want to echo Today and otherwise I want to Display the Date of the last edit:
$timefromdb = '2015-01-17 08:55:34.000000'
$edit = strtotime($timefromdb);
if($edit > $_SERVER['REQUEST_TIME']){echo "Today";}
else{
echo strftime("on %A, the %d %B %Y", $edit);
}
echo " at ".date('h:i',$edit)
It always Displays 01/01/1970. There must be a Problem with strtotime. I did a bit of research and it seems like my Timestamp Format isn't a valid one: http://php.net/manual/en/datetime.formats.php
Around the web are a lot of Questions about converting Timestamps but I just can't find the right one: I also got a bit confused by all the functions to convert date stuff.
So can someone Tell me how to get a valid Timestamp for using it in strftime and to compare it to the REQUEST_TIME.
Thanks in Advance!
UPDATE: As Always: The Problem sits in Front of the PC. I declared the Variable but never assgined the Timestamp to it :)
Chop off the .000000 from the date as it makes the date a format strtotime() cannot work with. There's several ways to do this. A simple substr is one of them.
$timefromdb = substr('2015-01-17 08:55:34.000000', 0, -7);
I'm not exactly understood you, but
try
1. compare gettype( $edit ) and gettype($_SERVER['REQUEST_TIME'])
2. not sure what $timefromdb will be more then $_SERVER['REQUEST_TIME'], because IMHO when user edited data, time of it action will me less then current time.
How to take the time from date stored as 12/25/2012 5:12:05 AM .
date('l F j, Y, g:i a',strtotime($last_login_details[FL_DATETIME]));
This above function returned time as 12:00 am which should return 5:12AM.
FL_DATETIME has datatype DATE.
On database, the value is being stored like this :
12/25/2012 5:12:05 AM
According to the docs - http://docs.oracle.com/cd/B19306_01/server.102/b14220/datatype.htm#i1847 -
For input and output of dates, the standard Oracle date format is DD-MON-YY
That is most likely why $last_login_details[FL_DATETIME] is echoing 25-DEC-12
Try changing your query using TO_CHAR()
SELECT TO_CHAR(FL_DATETIME, 'MM/DD/YYYY HH24:MI:SS A.M.') AS FL_DATETIME ...
see http://infolab.stanford.edu/~ullman/fcdb/oracle/or-time.html#date format
Solved my problem by :
SELECT TO_CHAR(FL_DATETIME, 'DD.MM.YYYY:HH24:MI:SS') FROM "FMS_LOG"
First of all, in my opinion, you should be storing all dates as unix timestamps. This makes it lot easier for you to do searches against times, and removes any inconsistencies that may arise from date string manipulation.
Second, I tested your code; it looks to be OK from what I can tell. Echo out what you are getting in the $last_login_details[FL_DATETIME] variable. The issue may lie in the variable assignment, and not the date string manipulation.
Hope that helps!
how to compare this date format in PHP ? 30-APR-12 03.46.59.000000000 PM
You could create a DateTime object from that format and then get the date in any format you want - http://us2.php.net/manual/en/datetime.createfromformat.php
you could use the DateTime function createfromformat() (here) . If you make a timestamp out of that (using strtotime or getTimeStamp) it is easy to compare to other timestamps.
I am not sure what the last 9 digits are for. If it's milli seconds php supports just up to 6 digits with u thats why I'd used * to drop the last digits.
$date = DateTime::createFromFormat('j-M-y H.i.s.u*', '30-APR-12 03.46.59.000000000 PM');
Further information can be found here: http://php.net/manual/en/datetime.createfromformat.php
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.
I am trying to display a time I have in my database. I managed to have it display a time in the correct format for what I need, but for some reason, it is only displaying '4:00' every time.
Here is my code:
date('g:i', strtotime($row['startTime']))
An example of I have the time displayed in my database is like this: 00:12:30
Why is it showing '4:00' every time?
strtotime expects a datetime format ... you should do
date('g:i', strtotime('01 January 2009 ' . $row['startTime']))
Whats the underlying database, and what datatype does the startTime column have? Peering at the closest php code I have, strtoime works fine with a DATETIME representation in the DB (MySQL).
strtotime converts a date time string to a Unix timestamp.
Perhaps your $row['startTime'] doesn't qualify as a date time string.
None of the examples here discussed a date time string which did not include a date.
The link also said that if strtotime is confused, it returns random results. I would add a few more format characters and see what else is returned.
As noted the problem is the use of strtotime(). The following works on my machine, if it's of any use:
$date_text = $row['startTime']; // assuming the format "00:12:30"
list($hrs,$mins,$secs) = explode(":",$date_text); // in response to the question in the comments
/* the explode() takes the string "00:12:30" and breaks into three components "00","12" and "30".
these components are named, by their order in the array formed by explode(), as $hrs, $mins and $secs.
see: http://us3.php.net/manual/en/function.explode.php
and: http://us3.php.net/manual/en/function.list.php
*/
echo "<p>" . date("g:i",mktime($hrs,$mins,$secs)) . "</p>";