Quick question about date formatting php/javascript - php

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

Related

How to match string values of datetime format in both Jquery and PHP?

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

PHP picking data from a string

Basically I am wondering how I would go about taking pieces of a string and putting them into another variable. I need to do this as, from API I am using, you are given the date of channel creation and I would like to take the data from this, but it is displayed in a strange way.
Here is what is given by the API:
2012-06-11T13:36:21Z seconds
^^ That is what I need to change so that I can display it in a nicer way. i.e. 11-06-2012.
Thanks.
P.S. This is twitch API if it matters.
You could use two PHP functions: "strtotime()" and "date()"
$timestamp = "2012-06-11T13:36:21Z";
echo date("jS F, Y", strtotime($timestamp));
The above would echo "11th June, 2012"
Look up the createFromFormat() function for DateTime.

Date Showing up as "1969-12-31" in DB

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

How to get time with respect to MySQL timestamp, in ActionScript 3?

I want to show an ActionScript 3.0 Timer, with respect to a timestamp gotten from MySQL.
So suppose MySQL (PHP) returns a string like $my_birthday="2013-05-22 12:30:45"
I got it from ActionScript by a URLLoader, and I want to show the timer like this:
My age:
01 hours 01 minutes 42 seconds
What function should I use in:
public function timerHandler(e:TimerEvent)
{
log_textfield.text = String((new Date()).time) - my_birthday; // I need to convert the Date().Time to unix timestamp I guess, and a function for time difference.
}
This answer has a TimeSpan class that you may want to use. The code below should do what you need to get the TimeSpan where you can get the parts you need to display. I don't have Flash on this computer though to test, so your mileage may vary :)
// new Date() is allergic to dashes, it needs slashes.
my_birthday = my_birthday.replace('-', '/');
var sinceBirthday = TimeSpan.fromDates(new Date(), new Date(my_birthday));

NSDate and PHP echo out

i use the Predicate Editor to make Filter and save results to mysql.
Dates will be saved (Today) as: 316077618.500794
My Question:
How can i calculate this in a PHP Time? I want to echo out this date as a Realdate but i find no way how to calc this for php.
I cant modify in cocoa code, it must be format out in PHP.
I need help :)
Thank you very much.
NSDate Reference Tells me :
The sole primitive method of NSDate, timeIntervalSinceReferenceDate, provides the basis for all the other methods in the NSDate interface. This method returns a time value relative to an absolute reference dateā€”the first instant of 1 January 2001, GMT.
You have 2 options:
Modify your cocoa code to export the date in a familiar format. (Yes, this may not be an option, but it will honestly be the easiest)
Do the calculation yourself. There is danger in this, in that you've got to be sure that you don't mess up the timezone. In it's simplest, it's 978307200 + NSDate.
oh thank you :)
The Soulution is:
$NSDATE='-61737774.586439';
$d=mktime(0, 0, 0, 1, 1, 2001)+ ($NSDATE) ;
echo date("d.m.Y",$d);
:D
Hope helps :)

Categories