Php datetime get time
- How to get the current Date and Time in PHP ?
- PHP program to change date format
- How to convert DateTime to String using PHP ?
- How to get Time Difference in Minutes in PHP ?
- Return all dates between two dates in an array in PHP
- Sort an array of dates in PHP
- How to get the time of the last modification of the current page in PHP?
- How to convert a Date into Timestamp using PHP ?
- How to add 24 hours to a unix timestamp in php?
- Sort a multidimensional array by date element in PHP
- Convert timestamp to readable date/time in PHP
- PHP | Number of week days between two dates
- PHP | Converting string to Date and DateTime
- How to get last day of a month from date in PHP ?
PHP String Based
- PHP | Change strings in an array to uppercase
- How to convert first character of all the words uppercase using PHP ?
- How to get the last character of a string in PHP ?
- How to convert uppercase string to lowercase using PHP ?
- How to extract Numbers From a String in PHP ?
- How to replace String in PHP ?
- How to Encrypt and Decrypt a PHP String ?
- How to display string values within a table using PHP ?
- How to write Multi-Line Strings in PHP ?
- How to check if a String Contains a Substring in PHP ?
- How to append a string in PHP ?
- How to remove white spaces only beginning/end of a string using PHP ?
- How to Remove Special Character from String in PHP ?
- How to create a string by joining the array elements using PHP ?
- How to prepend a string in PHP ?
PHP Class Based
PHP JSON Based
PHP File Systems Based
How to get time and date from datetime stamp in PHP?
I have one string like 8/29/2011 11:16:12 AM . I want to save in variable like $dat = ‘8/29/2011′ and $tme = ’11:16:12 AM’ How to achieve that? Can you give me example?
7 Answers 7
format('m/d/Y'); $time = $dt->format('H:i:s'); echo $date, ' | ', $time;
edit: To keep the AM/PM format use
You could use the strtotime function, as long as the dates are after 1/1/1970 —
strtotime creates a UNIX timestamp from the string you pass to it.
For more information about date() function, plz visit http://php.net/manual/en/function.date.php
This is probably not the cleanest way of doing it, but you can use an explode (note that there is NO validation at all here). It will be faster than a proper date manipulation.
$str = '8/29/2011 11:16:12 AM'; $dates = explode(' ', $str); $dat = $dates[0]; $time = $dates[1];
If your version of PHP is new enough, check out date_parse() and the array it returns. You can then format date or time portions using the relevant entries.
$mystring_datetime = . ; $dt = DateTime::createFromFormat('m/d/Y H:i:s A', $mystring_datetime ); $d = $dt->format('m/d/Y'); $t = $dt->format('H:i:s A');
You could also do something like this but it’s not preferred way:
$mystring_datetime = . ; list($date, $time) = explode(' ', $mystring_datetime, 2);
Now, $date and $time have appropriate values.
How to get time from datetime
I have datetime 30-12-1899 9:25:52 AM here needed only time that is 9:25:52 AM .this time i wanted to insert in mysql database and this field has data type time. my code is :
7 Answers 7
$s = '30-12-1899 9:25:52 AM'; $dt = new DateTime($s); $date = $dt->format('d-m-Y'); $time = $dt->format('h:i:s A'); echo $time;
actually the code does exactly what you want
$date = "1899-12-30 9:25:52 AM"; echo $start_time = date("h:i:s A", strtotime($date));
$date = "30-12-1899 9:25:52 AM"; $dtime = new DateTime($date); echo $dtime->format("h:i:s A");
You get 01:00:00 as result, because on your version of PHP, strtotime() function returns invalid integer. More likely value false is returned, and when you format false via date() function, you will get 1970-01-01 00:00:00 . You get time 01:00:00 because you have timezone offset, you are probably in UTC+1 timezone.
Here you can see, that your code will not work correctly on PHP version bellow 5.2.6 , even on x64 machines, and results will be unreliable.
Best option would be to upgrade your PHP version (or change webhosting). But if you do not have this options, you can use string functions to break and concatenate date & time parts, like this:
$date = "30-12-1899 9:25:52 AM"; sscanf($date, "%d-%d-%d %d:%d:%d %s", $d, $m, $Y, $g, $i, $s, $A); $h = $A == 'AM' ? $g : $g + 12; echo "$Y-$m-$d $h:$i:$s"; # 1899-12-30 9:25:52 echo "$h:$i:$s"; # 9:25:52