从sql数据库表中提取订阅日期,我们希望客户在一年后的月底之前付款。订阅只是一个月内的某个日期。一年后的部分很简单,找出这个月底的位置,然后在几秒钟内把它加到一年的时间里,就会给我带来问题。
该日期存储为unix地面零点的秒数。如何找到从该值到月底的秒数?我尝试使用m-i-Y将日期值转换为实际日期
月底:
$expDate = date('m-i-Y',$row1["renewDate"]);这个很管用。我得到了那个月的最后一天的字符串形式。但如果我尝试:
$endOfMonth = strtotime($expDate);不管用..。
echo‘’ing $expDate以字符串形式显示月份的最后一天。
回声$endOfMonth什么也不回..。
谢谢你对这件事的想法。
发布于 2019-06-19 15:59:50
strtotime在任意日期格式下不能正常工作。你有两个选择:
date_create_from_format解析自定义日期格式。Y-m-d格式,strtotime将其自动理解。例如:
$date = date('Y-m-t', $row1["renewDate"]);
$timestamp = strtotime($date);如注释中所提到的,您应该使用t而不是i。
发布于 2019-06-19 16:35:08
你可以玩这样的游戏。如果数据库具有Epoch时间,则可以使用“@”符号将其转换为日期。您可以这样获得订阅日期,也可以使用m/t/Y获得订阅月份的结束日期。您可以使用get时间戳将其转换回DT,然后转换为UNIX时间。看起来它在时间= 1560961801的时候起作用。
$row1["renewDate"] = 1560961801;
$unixfromDB = $row1["renewDate"];
$date = new DateTime('@' .$unixfromDB); // your UNIX timestamp.
$subdate = $date->format( 'm/d/Y' ); // subscription date
$endmonth = $date->format( 'm/t/Y' ); // end of month date
$endmonth = DateTime::createFromFormat('m/d/Y', $endmonth);
$endmonth = $endmonth->getTimestamp(); // UNIX timestamp for end of month.
echo ($endmonth - $unixfromDB) / (60 * 60 *24); // days to end of month发布于 2019-06-19 17:22:04
尝试使用mktime()而不是strtotime()。
<?
/* establish variables */
$now=time(); // epoch seconds right now
$expDate = date('m-t-Y', $row1["renewDate"]); //note the switch to 't' as suggested by @ehymel
$eom = mktime($expDate); // converts 'end of month' date into epoch seconds
/* results */
$secondsleft = $eom - $now; // number of seconds until the end of the month
echo $secondsleft; // return results
?>https://stackoverflow.com/questions/56671629
复制相似问题