I have a json object which provides me the date in some kind of UTC format .
"date":new Date(Date.UTC(2013,1,5,13,53,47))
How can store this in mysql and what kind of column type should I use ? I'm using PHP to retrieve / store the data
Store this in the format of YYYY-mm-dd. You can't quite do this in a single line, so try this:
var d = new Date(Date.UTC(/* your stuff */));
...
"date": d.getFullYear() + '-' + d.getMonth() + '-' + d.getDate()
EDIT:
On the PHP side say that date is in $_POST['date']. Use date('Y-m-d', strtotime($_POST['date'])) to get the value to store in MySQL.
Related
So i am trying to get the current Discord Webhooks's embedded Timestamp with the following format YYYY-MM-DDTHH:MM:SS.MSSZ(PHP) which will result in 2015-12-31T12:00:00.000Z
What I have done:
$timestamp = date('Y-m-d') . 'T' . date("H:i:s") . '.';
But really I couldn't complete that format MSSZ which results in 000Z
You can display milliseconds (added in PHP 7.0.0) like this.
echo date('Y-m-d').'T'.date("H:i:s").'.'.date("v").'Z';
wich will output the following:
2018-03-08T06:57:24.000Z
Another code to display milliseconds if using DateTime
(new DateTime())->format('Y-m-d\TH:i:s\.v\Z');
I am writing a PHP script to send data to a CloudKit database via CK Web Services. It works well with string data, but I am having trouble passing a date from PHP to a CK Date/Time field.
CK returns dates as a 13-digit TIMESTAMP. So, the following code
$timestamp = $record['created']['timestamp'];
$timestamp = $timestamp/1000;
echo '<td>'.date('m-d-Y H:i:s', $timestamp)."</td>";
echoes out
04-28-2017 12:35:19
Fine and dandy.
So I make the assumption that if CK delivers a 13-digit TIMESTAMP it should accept the same when passed to a Date/Time field.
Alas, passing $dobTimestamp from the following
$dobTimestamp = strtotime($dob) * 1000;
Results in this BAD_REQUEST error
Invalid value, expected type TIMESTAMP
When I go to the CK Dashboard and manually enter $dob, CK returns a value exactly equal to $dobTimestamp so I think passing $dobTimestamp should work . . . but it does not.
I cannot find out what I am supposed to do in Apple's docs. Does anyone know how to pass a date to a CK Date/Time field via Web Services? Hard to imagine there would not be a way to do it.
After a lot of fiddles, here's what works using PHP DateTime:
$openingDate = $_POST['openingDate']; // is mm/dd/yyyy
$closingDate = $_POST['closingDate'];
. . .
// Process Dates
$openingDateTime = new DateTime($openingDate, new DateTimeZone('America/New_York')); // Create new DateTime object
$closingDateTime = new DateTime($closingDate, new DateTimeZone('America/New_York'));
date_time_set($openingDateTime, 10, 00); // Set time for date object
date_time_set($closingDateTime, 17, 00); // Set time for date object
$oDate = (date_timestamp_get($openingDateTime)*1000); // Get the TIMESTAMP from DateTiem object and convert to milliseconds
$cDate = (date_timestamp_get($closingDateTime)*1000);
The JSON for the CK Query is simple:
. . .
"openingDate": {"value":'.$oDate.'},
"closingDate": {"value":'.$cDate.'},
I am using facebook apis and getting created_time as
2012-06-06T16:20:43+0000
I have posted this status at about
21:57 (IST)
and i am getting 2012-06-06T16:20:43+0000
any help will be appreciated, to get the same time as i have posted to get the exact updates.
you can convert any timezone using this code:
$timestamp = strtotime("2012-06-06T16:20:43+0000"); //here you put your string with the tie
$dtime = new DateTime();
$dtime->setTimestamp($timestamp);
$localtz = new DateTimeZone("Asia/Calcutta"); //choose the correct PHP timezone
$dtime->setTimeZone($localtz); //we apply the timezone
$stringtime = $dtime->format('Y-m-d\TH:i:sO'); //here you return the time in the same format as facebook
$unixtime = $dtime->format('U'); //here u get the unix timestamp format of the same string
print $stringtime;
I've written a function earlier for this task,
I've updated it for your needs,
var fbDateFix = function(date){
var local = new Date(date.replace(/-/g,'/').replace('T',' ').replace('+0000',''));
local.setSeconds(local.getSeconds() + 19800);
return local;
}
var padZero = function(t){
if(t<10){
return '0' + t;
}
return t;
}
var d = fbDateFix('2012-06-06T16:20:43+0000');
console.log(d);
var month = padZero(+d.getMonth()+1);
var date = padZero(+d.getDate());
var hour = padZero(+d.getHours());
var min = padZero (+d.getMinutes());
var sec = padZero (+d.getSeconds());
console.log(d.getFullYear() + '-' + month + '-' + date + 'T' + hour + ':' + min + ':' + sec + '+0000')
Edit:
This works for me in php,
date_default_timezone_set('Asia/Calcutta');
$timestamp = strtotime('2012-06-06T16:20:43+0000');
$local_datetime = date('c',$timestamp);
echo $local_datetime;
It looks like the date returned to you is a UTC datetime. You need to convert it to your local timezone (IST)...
See the following SO question for converting datetimes using php:
php convert datetime to UTC
You can use PHP's strtotime to convert created_time to a UNIX timestamp. Then you can add or subtract your time zone offset (IST is UTC+05:30).
I have a simple textfield with the type defied to be as date which attached with a jquery datepicker(). Now i have set the dateFormat() to be dd-mm-yy. All this works fine. But as i know MySql requires date in yyyy-mm-dd. So i was just wondering if i can convert that date format either using php or javascript to enter into the database.
E.g var date = $('#order_date').val(); gives value 26-2-2012. I need to convert this into 2012-02-26.
Note: There is also a 0 being added to the month.
Use PHP:
$date = $_POST['date']; // e.g. 06-08-2011
$mysql_date = date('Y-m-d', strtotime($date));
or
$date = explode('-', $_POST['date']);
$mysql_date = $date[2].'-'.$date[1].'-'.$date[1];
use $mysql_date to insert into the database.
You can do in javascript by parsing the input and loading it into a Date() object, and then using the getMonth, getDate, and getFullYear functions, as follows:
//Assume input is dd-mm-yyyy
var dateParts = $('#order_date').val().split("-");
var date = new Date(dateParts[2], (dateParts[1] - 1), dateParts[0]);
//format using getMonth(), getDate() and getFullYear() methods)
var formattedMonth = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1);
var formattedDate = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
var output = date.getFullYear() + '-' + formattedMonth + '-' + formattedDate;
You can see more at this excellent StackOverflow post
did u try date.format("yyyy-mm-dd"); ?
u can also use "isoDate" in place of "yyyy-mm-dd"
and if u are using mysql u can use DATE_FORMAT(date,'%Y-%m-%d')
i am trying to retrieve date from Mysql db to my html form in order to edit the records, now the way i used to insert the date into the database was getting Year - Month - Day each from a list and then unite them in one variable like this :
$dob = $_POST['Year'] . '-' . $_POST['Month'] . '-' .$_POST['Day'];
this will insert the value of $dob like this format 0000-00-00 in my table
the problem is that how i will retrieve this variable and split it each element to its specific list
what i tried seems useless and wrong which is this code
$row['userDateOfBirth'] = $year . '-' . $month . '-' . $day ;
then put each variable to (example:year)
<option value="1920" selected="selected"><? echo $year ; ?></option>
this did not work , how can i do this ?
Assuming you've got an actual date/datetime field, you can do the splitting inside MySQL:
SELECT YEAR(datefield), MONTH(datefield), DAY(datefield), etc...
which gives you three separate fields containing the individual date components. You could also do things like explode('-', $datestr) in PHP to decompose 'yyyy-mm-dd' into individual yyyy, mm, and dd chunks.
Lots of options, it's up to you to pick which one is easiest/best for your particular problem.
You can handle that on the client (php) side.
$year = date('Y', strtotime( $row['userDateOfBirth'] ) );
if you have an invalid date, $year will have 1970.
You could use
$row['userDateOfBirth'] = date("Y-m-d", strtotime($mysqlResultRow['dateField']));
Where basically you're telling the date() function to return a formatted string of the time passed to it, in this case created via strtotime().
Date() reference. The above example returns the date in "2000-01-01", see the reference for selecting the appropriate format for your project.