无法从字符串

时间:2017-08-22 11:34:35

标签: php date

我在字符串中的日期如下:

$rawDate = "08/08/2017 - 21/09/2017";

为了将两个日期彼此分开,我使用了代码:

$dates = explode("-", $rawDate);

现在我想将日期打印为Date1和Date2,如下所示:

$date1 = date('Y-m-d', strtotime($dates[0])); 
echo "Date 1: " . $date1;
echo "<br/>";
$date2 =  date('Y-m-d', strtotime($dates[1]));
echo "Date 2: " . $date2;

我想把结果作为
日期1:2017-08-08
日期2:2017-09-21

但我得到的结果是 日期1:2017-08-08
日期2:2017-01-01

任何人都可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:6)

如果您使用DateTime对象,使用日期可以轻松生活,因为您可以轻松指定您接收日期的格式

$rawDate = "08/08/2017 - 21/09/2017";

$dates = explode("-", $rawDate);

$date1 = DateTime::createFromFormat('d/m/Y', trim($dates[0])); 
echo "Date 1: " . $date1->format('Y-m-d');
echo PHP_EOL;
$date2 = DateTime::createFromFormat('d/m/Y', trim($dates[1])); 
echo "Date 2: " . $date2->format('Y-m-d');

答案 1 :(得分:0)

试试这个..

var entries = [{name: 'Test1', size: 1234}, {name: 'Test2', size: 324563425}, {name: 'Test3', size: 321453452345}, {name: 'Test4', size: 789078}]

var drawChart = function(entries, elementId) {

    var options = {
        width: "100%",
        height: 148,
        fontSize: 8,
        tooltip: { textStyle: { bold: true, color: '#000000', fontSize: 13 }, showColorCode: true, isHtml: true, ignoreBounds: true, text: 'both', trigger: 'selection' },
        legend: { position: 'right', textStyle: { fontSize: 10 } },
        chartArea: { left: 5, top: 10, right: 5, bottom: 10, height: "148", width: "100%" },
        sliceVisibilityThreshold: 0,
        pieSliceText: 'none',
        //pieHole: 0.4,
    };

    var chart = new google.visualization.PieChart(document.getElementById(elementId));

    var data = new google.visualization.DataTable();

    data.addColumn('string', 'Name');
    data.addColumn('number', 'Size');
    data.addColumn({ type: 'string', role: 'tooltip' });

    data.addRows(entries.length);
    var i = 0;
    $.each(entries, function () {

        data.setCell(i, 0, this.name);                    
        data.setCell(i, 1, this.size);
        // How to make this display correctly?
        // If it stays like this i lose percentage and legend name from tooltip
        data.setCell(i, 2, formatBytes(this.size)); 

        i++;
    });

    chart.draw(data, options);
}

var formatBytes = function (bytes, precision) {
    if (isNaN(parseFloat(bytes)) || !isFinite(bytes)) return '-';
    if (typeof precision === 'undefined') precision = 1;
    var units = ['bytes', 'kB', 'MB', 'GB', 'TB', 'PB'],
        number = Math.floor(Math.log(bytes) / Math.log(1024));
    return (bytes / Math.pow(1024, Math.floor(number))).toFixed(precision) + ' ' + units[number];
};