jasonc Posted April 26, 2010 Share Posted April 26, 2010 What I am trying to do is grab the date part and format it as so.. dd-mm-yyyy this is my code which shows the error... Warning: date_format() expects parameter 1 to be DateTime, string given in **** on line 25 how would i correctly get the date part and format it. $post_time = "2010-04-26 18:12:00"; $datestring = date_format($post_time, 'd-m-Y'); Link to comment https://forums.phpfreaks.com/topic/199808-can-not-format-the-date-correctly-please-help/ Share on other sites More sharing options...
andrewgauger Posted April 26, 2010 Share Posted April 26, 2010 I think you need to instantiate an object here. $post_time = new date("2010-04-26 18:12:00"); $datestring = date_format($post_time, 'd-m-Y'); Link to comment https://forums.phpfreaks.com/topic/199808-can-not-format-the-date-correctly-please-help/#findComment-1048769 Share on other sites More sharing options...
de.monkeyz Posted April 26, 2010 Share Posted April 26, 2010 Use strftime and strtotime $post_time = "2010-04-26 18:12:00"; $time = strotime($post_time); //Convert to timestamp! $datestring = strftime('%d-%m-%Y', $post_time); Or as andrew said, use a date object. Link to comment https://forums.phpfreaks.com/topic/199808-can-not-format-the-date-correctly-please-help/#findComment-1048770 Share on other sites More sharing options...
jasonc Posted April 26, 2010 Author Share Posted April 26, 2010 Use strftime and strtotime $post_time = "2010-04-26 18:12:00"; $time = strotime($post_time); //Convert to timestamp! $datestring = strftime('%d-%m-%Y', $post_time); Or as andrew said, use a date object. thank you i used this reply to fix my coding. Link to comment https://forums.phpfreaks.com/topic/199808-can-not-format-the-date-correctly-please-help/#findComment-1048775 Share on other sites More sharing options...
andrewgauger Posted April 26, 2010 Share Posted April 26, 2010 Just remember that you will be limited to the timestamp range of 1970 - 2038 so as long as you aren't doing comparison to future or past events (such as birthdays) that way is more efficient. Link to comment https://forums.phpfreaks.com/topic/199808-can-not-format-the-date-correctly-please-help/#findComment-1048783 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.