Jump to content

Help with dates and time.


takn25

Recommended Posts

Hi, I have a created comment system, now what i am after is when the comment is posted the time is posted in hidden format. All is going fine but what i don't know, what should the time be posted as example date(H:m:s) or... the result i want is time in minutes first e.g.

commented 2 mins ago and increasing to hours after that days. Could some one kindly guide me in the right directions.

Link to comment
https://forums.phpfreaks.com/topic/226215-help-with-dates-and-time/
Share on other sites

<?php
$date = '31-01-2011';
$beggin_hour = '13:30';
$end_hour = '14:00';


// Hours
define("SECONDS_PER_HOUR", 60*60);
	// Calculate timestamp
	$start = strtotime($date." ".$beggin_hour);
	$stop = strtotime($date." ".$end_hour);

	// Diferences
	$difference = $stop - $start;

	// Hours
	$hours = round($difference / SECONDS_PER_HOUR, 0, PHP_ROUND_HALF_DOWN);
	#
// Minutes

$minutes = ($difference % SECONDS_PER_HOUR) / 60;

echo $hours. ":" .$minutes;

?>

If the time will be stored in a database, use the native format; typically YYYY-MM-DD HH:MM:SS, and store it in a field meant to hold date/time data. Then format it how you want it when you display it. If you're using MySQL, storing the date/timestamp is as simple as using NOW() as the value in the query string.

 

INSERT INTO `table` (`timestamp_field`) VALUES ( NOW() )

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.