将字符串转换为日期php [英] convert string to date php
问题描述
你好我想建造
我从xxx表格中获得$ date和$ date1.我想请假计划.
I get $date and $date1 from form xxx. I wanted to make leave program.
现在我想要处理$ date,变量字符串的值为xx-xx-xxxx(dd/mm/yyyy), 现在我想将具有值的$ date1转换为日期.
now i wanted process $date with variable string with value xx-xx-xxxx(dd/mm/yyyy)and $date1 with value now i want to convert them to date.
我已经知道如何使用datediff()来计算天数
I already know how to count day using datediff()
我转换$ date,以便可以使用dateadd()函数
i convert $date so i can use dateadd() function
代码在这里
$t1 = substr($date,0,2);
$b1 = substr($date,3,2);
$y1 = substr($date,6,4);
$t2 = substr($date11,0,2);
$b2 = substr($date1,3,2);
$y2 = substr($date1,6,4);
$tawal ="$y1-$b1-$t1";
$takhir = "$y2-$b2-$t2";
$query = "SELECT datediff('$takhir', '$tawal')as selisih";
$hasil = mysql_query($query);
$data = mysql_fetch_array($hasil);
$times = $data['selisih'];
$times = + 1;
这是图片
推荐答案
您不需要substr或mysql.首先获取您的日期,不包含substr:
You don't need substr or mysql for this. First get your dates without substr:
$tawal = date('Y-m-d', strtotime($date));
$takhir = date('Y-m-d', strtotime($date1));
现在您有了Y-m-d格式化的字符串.要找到差异,尽管您不必转换为Y-m-d,因为我们不需要mysql.您可以使用此方法查找相差秒数.
Now you have the Y-m-d formatted strings. To find the diff, though you don't have to convert to Y-m-d since we don't need mysql. You can use this method to find the difference in seconds.
$diff = abs(strtotime($date2) - strtotime($date));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
这篇关于将字符串转换为日期php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!