2016-03-17 16:09
浏览 39


I have a string of a letter and then a month followed by a year:

$followUpDate = 'M12-16'

I break up the month and the year:

$datestrip = explode("-", $followUpDate);
$part1     = substr($datestrip[0], 1); // Gives Month
$part2     = $datestrip[1]; // Gives Year

Now that I have broken up the month and year. I need to get the unix timestamp of said month and year.

So for this example the result of the unix timestamp should be 1480568400.

Thank you in advance!

图片转代码服务由CSDN问答提供 功能建议


  $ followUpDate ='M12-16'


  $ datestrip = explode(“ - ”,$ followUpDate); 
 $ part1 = substr($ datestrip [0],1);  //给出月
 $ part2 = $ datestrip [1];  //给予年份

现在我已经打破了月份和年份。 我需要获取上述月份和年份的unix时间戳。


谢谢 你提前!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • doulidai6316 2016-03-18 11:00

    You can use DateTime::createFromFormat() (as other answers already mentioned). However, you have to pay attention to some minor details.

    Because you parse a string that contains a partial date (only the month and the year are specified), the rest of the fields (day of month, hour, minute, second) are set, by default, using the current time. Most probably, this is not what you want. I guess you want to get the start of the month (day 1, at midnight).

    You can get this by putting an exclamation mark (!) in front of the format string. It resets all the date components to the Unix epoch (1970-01-01 00:00:00 UTC).

    Also, the third parameter of DateTime::createFromFormat() is the timezone to use. If you don't pass it, PHP uses the default time zone set in php.ini or by the last call to date_default_timezone_set() (if there was any). This may or may not be what you need.

    $followUpDate = 'M12-16';
    // It seems like your timezone is US/Eastern (GMT+5 during the winter)
    $timezone = new DateTimeZone('US/Eastern');
    // Create a DateTime out of the provided string
    // The "!" character in front of the format resets all the fields
    // to the Unix epoch (1970-01-01 00:00:00 UTC) before parsing the string
    $date = DateTime::createFromFormat('!\Mm-y', $followUpdate, $timezone);
    // Display it; it displays 1480568400
    打赏 评论
  • doupao1978 2016-03-17 16:19

    Have a look at strtotime

    $timestamp = strtotime("2016-12");
    echo date("Y-m-d", $timestamp); // prints 2016-12-01

    keep your timezone-settings in mind ;)

    You could also go for a more readable approach using php's DateTime class:

    $date = new \DateTime();
    $date->setDate(2016, 12, 01);
    echo $date->format("Y-m-d"); // similiar to above
    打赏 评论
  • dongping1922 2016-03-17 16:26

    Made a few assumptions but the following code should work for you:

    $followUpDate = 'M12-16';
    $d = DateTime::createFromFormat('\Mm-y-d H:i:s', $followUpDate . '-01 00:00:00');
    echo $d->format('U');

    I'd definitely advise using DateTime over date() because of the extra flexibility it gives you.

    打赏 评论
  • dou4064 2016-03-18 08:45

    Below is a small code snippet that will help you on your merry way:

    // Define your date
    $followUpDate = 'M12-16'
    // Convert your date to a DateTime object
    $date = DateTime::createFromFormat('\Mm-y', $followUpdate);
    // Output
    echo $date->format('Y-m-d');
    打赏 评论

相关推荐 更多相似问题