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 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持