Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

mysql - convert string to date php

hello i wanted to build

I get $date and $date1 from form xxx. I wanted to make leave program.

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.

I already know how to count day using datediff()

i convert $date so i can use dateadd() function

Here the code

$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;

here the picture enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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));

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));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...