douchun1961 2018-01-31 04:24
浏览 53
已采纳

如何将年份字符串转换为PHP中的日期?

I am trying to convert a string into a date. Specifically, a string of a year into a date.

strtotime('2017') // Gives us -> 1517372220

$year = '2017';
var_dump($year); // '2017'
$year = date("Y", strtotime($year));
var_dump($year); // '2018' 

Why is the date method defaulting to the current time? I am passing an integer.

According to the documentation, I seem to implementing this method correctly?

string date ( string $format [, int $timestamp = time() ] )

The optional timestamp parameter is an integer Unix timestamp that defaults to the current local time if a timestamp is not given. In other words, it defaults to the value of time().

  • 写回答

1条回答 默认 最新

  • drcigvoy48900 2018-01-31 04:34
    关注

    Based on strtotime() Manual

    strtotime — Parse about any English textual datetime description into a Unix timestamp

    Parameters

    time-> A date/time string. Valid formats are explained in Date and Time Formats.

    So either you need to provide a textual representation or a valid date format (not only years).

    So code need to be like this:-

    <?php
    
    $year = date("Y", strtotime('2017-01-01'));
    var_dump($year); // '2018' 
    

    Output:- https://eval.in/945561

    Reference:- valid formats for strtotime()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 SPSS分类模型实训题步骤
  • ¥15 求解决扩散模型代码问题
  • ¥15 工创大赛太阳能电动车项目零基础要学什么
  • ¥20 limma多组间分析最终p值只有一个
  • ¥15 nopCommerce开发问题
  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
  • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
  • ¥15 pycharm输出和导师的一样,但是标红
  • ¥15 想问问富文本拿到的html怎么转成docx的
  • ¥15 我看了您的文章,遇到了个问题。