0

How can I create a date string in PHP that can be read by .NET's non standard date format:

eg Given this PHP code:

$fetch = mysql_query("SELECT birthday FROM people"); 
while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
    $birthday = $row['birthday'];
    $json = ??? // some way to convert it to the .net json format
}

How can I manipulate that PHP date to return this style of JSON as expected by C#/.Net's json deserialize method:

{"birthday":"\/Date(1331550000000)\/"}

2 Answers 2

3

The .NET format is Milliseconds from the Epoch - so the following format worked for me

 $date = '/Date('.(date("U",time())*1000).'-0500)/';
Sign up to request clarification or add additional context in comments.

Comments

0

.Net's json deserialize method can accept any json data. You could build the json like below.

$fetch = mysql_query("SELECT birthday FROM people"); 
$birthdays = array();
while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
    $birthdays[] = $row;
}
$json = json_encode($birthdays);
echo $json;

4 Comments

Unfortunately the remote server I am connecting to will only accept one exact date format: \/Date(1331550000000)\/ and throws an exception if any other format is sent.
@JK What is your birthday like?
Lol, thats a bit personal isnt it, I hardly know you :) (Its a standard mysql datetime column)
@JK SO you want to turn date to a timestamp? But you should know you should not use timestamp to present birthday.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.