Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
322 views
in Technique[技术] by (71.8m points)

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?

question from:https://stackoverflow.com/questions/9904080/how-to-get-time-and-date-from-datetime-stamp-in-php

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

E.g.

<?php
$s = '8/29/2011 11:16:12 AM';
$dt = new DateTime($s);

$date = $dt->format('m/d/Y');
$time = $dt->format('H:i:s');

echo $date, ' | ', $time;

see http://docs.php.net/class.datetime


edit: To keep the AM/PM format use

$time = $dt->format('h:i:s A');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
...