在 php 中创建一个 C#/.Net json 日期


Create a C#/.Net json date in php

如何在 PHP 中创建可由 .NET 的非标准日期格式:

例如给定这个PHP代码:

$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
}

我如何操作该 PHP 日期以按照 C#/的预期返回这种风格的 JSON。Net的json反序列化方法:

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

.NET 格式是纪元的毫秒 - 所以以下格式对我有用

 $date = '/Date('.(date("U",time())*1000).'-0500)/';

.Net的json反序列化方法可以接受任何json数据。您可以像下面这样构建 json。

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