https://quoteinvestigator.com/2019/03/05/sunshine/
Reply from Quote Investigator: QI believes that the ascription to Walt Whitman occurred because of a naming confusion error. First, a remark in this family was ascribed to M. B. Whitman by 1903. Second, by 1910 the remark was attributed to the single name Whitman without the initials. Third, Walt Whitman received credit by 1919.
A report in 1927 asserted that Helen Keller wrote an instance in an autograph album, but the saying was already in circulation. See detailed citations given further below.
QI conjectures that the saying evolved over time, and a significant nascency occurred in a verse by English poet Charles Swain published in The Literary Gazette of London in 1850. The poem Youth and Age employed the framework of sunlight and shadows mentioned above. The second verse which referred to youthful exuberance transitioning toward harsher maturity contained the core ideas of the saying under analysis in the two highlighted lines. Emphasis added to excerpts: