English 中文(简体)
PHP中的日期问题
原标题:Date problem in PHP
  • 时间:2011-02-07 00:26:27
  •  标签:
  • php
  • date

I have a piece of PHP that is trying to do this: 1) given a string like "h m s" (where h=hr, m=min, s=sec) 2) Add the time from 1) to time() 3) format the result to look like "y-mth-d-h-min-s"

所以说现在的时间是2011年1月1日凌晨1点,我希望它添加“10 0 0”,这应该给我2011年11月1日上午11点,但由于目前的某种原因,它似乎确实添加了字符串,但并不准确。

这是我正在使用的代码:

$values_arr[ regx_expdate ] = date("Y-m-d H:i:s", time()+$values_arr[ regx_expdate ]);

其中$values_arr[regx_expdate]是格式为“h m s”的字符串,例如“10 0 0”。

主要问题是,时间()如何知道“10 0 0”实际上是10小时0分钟0分钟,而不是10天0小时0分钟??

最佳回答

我能想到的最简单的方法是从$values_arr[regx_expdate]中提取每个令牌,将秒相加,然后简单地将其添加到time()

例如

if (preg_match( /^(d{1,2}) (d{1,2}) (d{1,2})$/ , $values_arr[ regx_expdate ], $units)) {
    $seconds = $units[3] + ($units[2] * 60) + ($units[1] * 3600);

    $newTimestamp = time() + $seconds;
}
问题回答

事实并非如此。

它将将其强制转换为int,将其解释为秒,并将其添加到time()的结果中。

有些代码可以按照您的描述执行:

list ($h,$m,$s) = explode(   , $values_arr[ regx_expdate ], 3);
$difference = 60*60*$h + 60*$m + $s;
$values_arr[ regx_expdate ] = date("Y-m-d H:i:s", time()+$difference);

在将输入字符串解析为Hours Minutes和Seconds之后,可能值得将所述值数组重新组织为PHPstrtotime可以处理。

这个小功能可能会有所帮助,您可以根据自己的目的进行自定义:

function AddingDaysFromNow($number_of_days)
    {
        $today = mktime(0, 0, 0, date( m ), date( d ), date( Y ));
         // today is now time return in seconds

        $addingTime = $today + (86400 * $number_of_days);
         // adding to advance it

        //choice a date form at here
        return date("Y-m-d", $addingTime);
    }

    //use it as 
      $expireDate = AddingDaysFromNow(2);  // assume the 2 is advance to 2 days ahead
                                           // return the day in future

祝你好运

要在php中处理和转换日期,您应该首先将所有内容强制转换为unixtimestamp,然后为其提供所需的结构

$date = date("THE-DATE-FORMAT-YOU-WANT", "THE-DATE-YOU-WOULD-LIKE-TO-CONVERT-IN-SECONDS");

//For example.

$new_date = date("Y-m-d H:i:s", strtotime($old_date));

$now = date("Y-m-d H:i:s", time());




相关问题
Mysql compaire two dates from datetime?

I was try to measure what is faster and what should be the best way to compare two dates, from datetime record in MySql db. There are several approaches how to grab a date from date time, but I was ...

iPhone Date Picker rolls over to 2010 on December 27th?

So I implemented a UIDatepicker in one of my applications for scheduling and autodialing teleconferences... everything is pretty much ready to go except, while testing I noticed that when the date ...

Convert date Python

I have MMDDYY dates, i.e. today is 111609 How do I convert this to 11/16/2009, in Python?

specifying date format when using $form->inputs() in CakePHP

I am wondering if there is a way to specify the date format in the forms created using CakePHP s $form->inputs(); Please note that this is not the individual $form->input() but instead $form->inputs() ...

NSDateFormat, super simple! Where am I screwing up?

Ok, this is really simple, maybe I m a getting a bit burnt out, but seems like it should work, Query XML feed, put out date string, format, display in a cell. The issue is I m a getting a NULL ...

sqlite writing a date into an email

I am exporting a date value from sqlite and placing it into an email. The date appears like this 279498721.322872 I am using Objective C in an Iphone App. Does anyone know how to make this export ...

热门标签