Jump to content

Formating Timestamp from PostgreSQL with PHP


lopes_andre

Recommended Posts

Hi,

 

I need to format a Timestamp date from PostgreSQL with PHP in Portuguese language just like this:

 

Sun, 31 Jan 2010 12:08:52

 

To show the current system date I use this function:

 

function FullDateAndTimePortuguese()
{
	setlocale(LC_ALL, NULL);
	setlocale(LC_ALL, 'pt_PT');
	$DayOfWeek = ucfirst(gmstrftime("%A %d %b %Y %H:%M", time ()));

	return $DayOfWeek;
}

 

But now I need to format the date from the database, how can I use the function?

 

If you can help me, please reply.

 

 

Best Regards,

you'll want to do two things (if you want to use that function). the first is to modify your query to use a function that will convert your timestamp column into a UNIX timestamp:

 

SELECT date_part('epoch', timestamp_column_name) AS UNIX_timestamp FROM table

 

second, you'll need to adjust the function so that you can pass it a timestamp, rather than it assuming you want the current date/time. this involves putting a parameter in the function which has a default value when not specified:

 

function FullDateAndTimePortuguese($timestamp = time())
{
  setlocale(LC_ALL, NULL);
  setlocale(LC_ALL, 'pt_PT');
  $DayOfWeek = ucfirst(gmstrftime("%A %d %b %Y %H:%M", $timestamp));
  return $DayOfWeek;
}

 

then you can use the function to format your date and time:

 

$formatted_date = FullDateAndTimePortuguese($timestamp_from_query);

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.