I'm trying to store a date value in a MySQL database using serialize(). However, the result in the db is treated as "1969-12-31". I'm almost certain it's because of the way the data is being serialized in my ajax call.
Here are the code snippets. Where am I going wrong?
Ajax portion:
data: decodeURIComponent(form.serialize()),
The Result portion of the serialized data is this (when I view the serialized data in console):
&pur-date=2014+/+02+/+31
^ I think the "+" is what's causing the error.
In my model (Codeigniter):
$date = date("Y-m-d", strtotime($this->input->post('pur-date')));
If I replace the strtotime value to "2014-10-10" for example, the data is correctly stored into the db. So the issue has to be related to the post data coming in.
Note, column type in db is date.
Anyone?
I'm not entirely sure what's going on because the test data (from the form) is not provided but here are some things I would immediately check for:
Javascript counts in milliseconds, Java counts in seconds, PHP counts in seconds: this is your most likely problem.
I don't know where 2014-02-31 is coming from. There was no February 31st this year or, for that matter, ever. This could potentially break things?
So, figured it out - thanks to the comments on my initial question. I was able to remove the white space from my string in my model. Post data was 2014 / 02 / 31, not 2014+/+02+/+31 as was displayed in browser console.
New code:
$format = preg_replace('/\s+/', '', $this->input->post('pur-date')); // formats string and removes whitespace
$date = date("Y-m-d", strtotime($format));
Related
For example -
In Jquery using the function (new Date()).getTime() am getting current datetime as 1470291303352.
But In PHP using strtotime(date('H:i:s')) am getting it as 1470291299.
Here i need to get the same string values. How to do it?
Firstly, php returns the number of seconds since 1970/01/01, jquery returns a number of milliseconds, so there is no way to be the same value.
Second - even if you've got the fastest server in the world it comes to the milliseconds in the execution of lines of code. So exactly the same value can hardly be achieved :)
What you can do to try to trim jquery for the last three numbers representing the milliseconds (this of course if you do these two lines of code to execute in one second :))
And for last, there is a issue of clocks on your server and client computer - it must be exactly the same.
The javascript method getTime() returns microseconds (http://www.w3schools.com/jsref/jsref_gettime.asp) whereas PHP time() (or in your case strtotime() http://php.net/manual/en/function.strtotime.php) returns seconds. The first depends on your clients clock, the latter on your servers clock...
Mostly you never will get the same timestamps this way... maybe you could work around using some kind of AJAX api to have the same timestamp on both sides...
Check this :
In php:
echo strtotime(date('H:i:s')); // 1470294647
In Script :
var date = new Date();
var d = Date.parse("'"+date+"'")/1000; // 1470294647
alert(d);
I have an ISO 8601 compliant date/time string 2014-12-11T23:43:44+00:00 which I’m passing over HTTP to a web server. There, I pull the data from my HTTP POST field and everything’s great, when I print_r my parameters, the value is returned as 2014-12-11T23:43:44 00:00.
What I notice though, is that the + is now missing, (I’m guessing this is in the presentation of the data rather than what the data is encoded as and isn’t(?) a problem, right?)
Then, on parsing the value as a parameter to create a new DateTime object, I get the following error:
DateTime::__construct(): Failed to parse time string (2014-12-11T23:43:44 00:00) at position 20 (0): Double time specification
I’m guessing this is because it sees the appending 00:00 as another time after the 23:43:44, when of course this is the time difference.
Is this because the + is missing or am I doing something else wrong?
I should also point out that my original value (2014-12-11T23:43:44+00:00) comes from http://www.timestampgenerator.com's ISO 8601 field.
Edit:
Here’s the code I use to get the string from the POST request in PHP:
static function request_parameters($including_request_uri = false)
{
$request_parameters = $_REQUEST;
if (!$including_request_uri)
{
unset($request_parameters[PARAMETER_NAME_REQUEST]);
}
return $request_parameters;
}
Then I retrieve the value like this:
$parameters = RESTController::request_parameters();
$date_created = $parameters[UNIVERSALLY_UNIQUE_OBJECT_KEY_DATE_CREATED];
You can always try doing trim(' ', str_replace(' ','+',$datetime)); to test. This will both strip whitespace on the outside and turn your inner space with an ISO8901 compliant time.
I believe your POST coming in is being interpreted as as an encoded space. Another way to do it (if you have control over whatever is sending is) POST with:
$datetime = urlencode($datetime);
Then on the receiving end:
$datetime = urldecode($_POST['datetime']);
I am using Spreadsheet_Excel_Reader to read .xls files in PHP.
Everything goes fine until it comes to reading a date. If I am reading a date field, it will always return the date as Nov 30, 1999 (or variations of this date depending upon the format). I have tried setting the OutputEncoding and it's giving the same result. I tried dumping the 'example.xls' that comes with the library and that also produces the same result.
Any help on a workaround for this would be highly appreciated.
You don't need to format your date in excel...if you have a date format in mind wrap it with double quote. e.g "13/04/1987" format("DD/MM/YYYY");
Spreadsheet_Excel_Reader will read this as a normal string with the double quote wrapper around it.
Then in your PHP make a query to remove and replace double quote with nothing.
$var = "13/04/1987";
$removeQuote = str_replace('"','',$var);
After this you will have to replace every occurence of the forwardslash (/) with hypen(-).
$removeSlashes = str_replace('/','-',$removeQuote);
Then use the date function in PHP to format it to your suitability.
$format = date('Y-m-d', strtotime($removeSlashes));
echo $format;
And you'r done... full code goes below.
$var = "13/04/1987";
echo date('Y-m-d',strtotime(str_replace('/','-',str_replace('"','',$var))));
I'm failing a bit to get this working.
I read out a date from my database in the yyyy-mm-dd format. However, I need to use it in my jQuery ajax call in the dd-mm-yyyy format. Is there a way to turn my dates around? I should've seen this coming when I started working on my app, but alas, I didn't :(
Don't feel like changing around the way I save stuff to my DB so I was wondering if anyone knows an easy way to change the format? Thanks :(
EDIT: Just ran into another, similar problem
I read time out as, for example, 08:00:00 I want to split this into parts aswell. For example
08:00:00 => var a = 8, var b = 00 // ignore seconds
09:30:00 => var a = 9, var b = 30
23:45:00 => var a = 23, var b = 45
10:30:00 => var a = 10, var b = 30
:( Thanks!
Format your date directly in your sql query :
SELECT DATE_FORMAT(fielddate,'%d-%m-%Y') as jsDate, DATE_FORMAT(fielddate,'%m-%d-%Y') as phpdate FROM xxx
You can do multiple format in the same query to fit your need (a format for js , one for php, one for direct display ...)
See date and time function
In PHP you can easily turn it around.
$date=date('d-m-Y', strtotime('2009-11-12'));
You could also achieve this using Javascript:
var date='2009-11-12'.split('-').reverse().join('-');
jsFiddle Demo
EDIT concerning your update:
var timeArray=myTime.split(':'); is what you need here. It grabs a string, and returns a normal array with the elements of the string splitted by :. So timeArray[0] will be the hour, timeArray[1] the minute.
Yes, you can turn it around, but yyyy-mm-dd is the internationally accepted way to represent dates in computers, so you really should not.
Instead, you should change your database, and if you want to present the date to the user in another format, you do the conversion for the presentation only.
EDIT: Sorry if this answer sounds rude, but I really believe that you will thank me later if you do this. Or at least keep it in mind until next time :)
use date function:
date("d-m-Y",strtotime($yourdate));
<?php
$newdate = date('d-m-Y',strtotime($db_date))
?>
Try this: jquery-dateFormat Plugin for jQuery
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>";