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

Categories

0 votes
1.9k 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...