Return to Snippet

Revision: 20724
at November 23, 2009 20:34 by chrisaiv

Initial Code
function strip_zeros_from_date( $marked_string = "" ){
		//Remove any Zeros in the date
		$no_zeros = str_replace('*0', '', $marked_string );
		//Remove the Asterik regardless of no Zeros
		$cleaned_string = str_replace('*', '', $no_zeros );
		return $cleaned_string;

	//Number of seconds since January 1, 1970
	echo $time = time();
	echo "<br /><br />";

	//Make a timestamp using mktime( $hr, $min, $sec, $mo, $day, $yr )
	echo $mktime =  mktime( 2, 30, 45, 10, 1, 2009 );
	echo "<br /><br />";

	//Convert this date to a string
	echo $date = strtotime("September 15, 2000");
	echo "<br /><br />";			

	//Convert Last Monday to a String
	echo $last_monday = strtotime("last Monday");
	echo "<br /><br />";			

	//Convert 1 day from today to a String
	echo $tomorrow = strtotime("+1 day");
	echo "<br /><br />";			

	//Check if this date is valid (February 31st)
	echo $valid_date = checkdate( 2, 31, 2000 ) ? "true" : "false";
	echo "<br /><br />";			
	//Format a Unix Timestamp into something human readable (
	//The asterik is neat little hack that will help us build a helper function
	echo strip_zeros_from_date( strftime( "*%m/*%d/%y", $last_monday ) );

	echo "<br /><br />";
	//Format your date in PHP for MySQL
	$dt = time();
	$mysql_datetime = strftime("%Y-%m-%d %H:%M:%S", $dt);
	echo $mysql_datetime;

Initial URL

Initial Description
Dates and Time are always important to understand in any language.  PHP has some rockin' functions but here are a few that I use the most.

Initial Title
PHP: Basic Date/Time Functions

Initial Tags

Initial Language