blinks58 Posted March 26, 2013 Share Posted March 26, 2013 I thought this one would be easy, but it isn't! I want to convert years only to YYYYMMDD format, e.g. 2010 becomes 20100101. But $date = date('Ymd', strtotime(2000)); throws up today's date, viz. 20130326. Any suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/276154-convert-yyyy-to-yyyymmdd/ Share on other sites More sharing options...
akphidelt2007 Posted March 26, 2013 Share Posted March 26, 2013 (edited) If you know the year... why don't you just do $date = $year.'0101'; Edited March 26, 2013 by akphidelt2007 Quote Link to comment https://forums.phpfreaks.com/topic/276154-convert-yyyy-to-yyyymmdd/#findComment-1421040 Share on other sites More sharing options...
blinks58 Posted March 26, 2013 Author Share Posted March 26, 2013 Thanks akphidelit2007. Yes, that is a simple solution. I just thought there would be a "proper" way to do it, rather than what is effectively a hack. Your solution does work. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/276154-convert-yyyy-to-yyyymmdd/#findComment-1421073 Share on other sites More sharing options...
haku Posted March 26, 2013 Share Posted March 26, 2013 $date = date('Y0101', strtotime(2000)); But there is nothing wrong with what the above poster gave. Either will work fine. Quote Link to comment https://forums.phpfreaks.com/topic/276154-convert-yyyy-to-yyyymmdd/#findComment-1421079 Share on other sites More sharing options...
salathe Posted March 26, 2013 Share Posted March 26, 2013 $date = date('Y0101', strtotime(2000)); But there is nothing wrong with what the above poster gave. Either will work fine. Nothing wrong at all, except it will print the value for January 1st of the current year. When given to strtotime(), the 2000 is interpreted as a 24-hour time: 8pm. I just thought there would be a "proper" way to do it, rather than what is effectively a hack.A good way would be something like the following, which uses DateTime::createFromFormat(). $date = DateTime::createFromFormat('!Y', $year)->format('Ymd'); If you're still stuck on PHP 5.2 or lower (I'm sorry!) then mktime() is an alternative. $date = date('Ymd', mktime(0, 0, 0, 1, 1, $year)); Quote Link to comment https://forums.phpfreaks.com/topic/276154-convert-yyyy-to-yyyymmdd/#findComment-1421200 Share on other sites More sharing options...
Jessica Posted March 26, 2013 Share Posted March 26, 2013 Thanks akphidelit2007. Yes, that is a simple solution. I just thought there would be a "proper" way to do it, rather than what is effectively a hack. Your solution does work. Thank you. How is it a "hack"? You have a year as a string. You want to print that year plus "0101". Literally that is as simple as concatenating your two strings. It's like if you said you had a last name and wanted to display the first name as "joe" for every last name. echo "Joe ".$lastname; If you want it in a format other than a STRING then you'll want to use fancier functions, but so far your post only indicated adding the string of "0101". Quote Link to comment https://forums.phpfreaks.com/topic/276154-convert-yyyy-to-yyyymmdd/#findComment-1421201 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.