johng Posted February 28, 2007 Share Posted February 28, 2007 I would like to parse out a date in the format (Y-m-d) and get integers for each of them. Does anyone know how to do this? Link to comment https://forums.phpfreaks.com/topic/40534-solved-parsing-a-date/ Share on other sites More sharing options...
kenrbnsn Posted February 28, 2007 Share Posted February 28, 2007 You can use the expode() function: <?php $date = '2007-02-28'; $list($year,$month,$day) = explode('-',$date); ?> Ken Link to comment https://forums.phpfreaks.com/topic/40534-solved-parsing-a-date/#findComment-196148 Share on other sites More sharing options...
Orio Posted February 28, 2007 Share Posted February 28, 2007 You can use explode(): <?php //$date holds the date list($year, $month, $day) = explode("-", $date); echo "Year: ".$year."<br>Month: ".$month."<br>Day: ".$day; ?> Edit- Ken is faster Orio. Link to comment https://forums.phpfreaks.com/topic/40534-solved-parsing-a-date/#findComment-196151 Share on other sites More sharing options...
johng Posted February 28, 2007 Author Share Posted February 28, 2007 Thank you both very much! Do you know if there is any way that I would be able to put that back into the same date format, only adding one day? I tried using mktime, but it apparently doesn't like variables. Or if you know of an easier way, that would be awesome! Thanks! Here's the code I tried to use: <?php list($year, $month, $day) = explode("-", $eserve_date); echo "Year: ".$year."<br>Month: ".$month."<br>Day: ".$day; $cst_date = date("Y-m-d", mktime(0,0,0, "$month", "$day", "$year"); ?> Before I did the last line, it ran just fine, and had numbers in $month, $day, and $year. Link to comment https://forums.phpfreaks.com/topic/40534-solved-parsing-a-date/#findComment-196177 Share on other sites More sharing options...
Orio Posted February 28, 2007 Share Posted February 28, 2007 Try this: <?php $eserve_date = "2007-28-2"; list($year, $month, $day) = explode("-", $eserve_date); echo date("Y-m-d", strtotime($day."/".$month."/".$year)+(24*60*60)); ?> Orio. Link to comment https://forums.phpfreaks.com/topic/40534-solved-parsing-a-date/#findComment-196186 Share on other sites More sharing options...
johng Posted February 28, 2007 Author Share Posted February 28, 2007 Thank you so much!!!!! It now works like it is supposed to! (One minor change that I had to make, I switched $day and $month, but a very minor detail when compared to the help you have given me) Link to comment https://forums.phpfreaks.com/topic/40534-solved-parsing-a-date/#findComment-196200 Share on other sites More sharing options...
obsidian Posted February 28, 2007 Share Posted February 28, 2007 If you're simply adding one day to a date like that, check out strtotime(): <?php $date = "2007-02-14"; echo date('Y-m-d', strtotime("$date +1 day")); ?> Link to comment https://forums.phpfreaks.com/topic/40534-solved-parsing-a-date/#findComment-196218 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.