Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,158,972 members, 7,838,472 topics. Date: Thursday, 23 May 2024 at 10:47 PM

Php: Converting Timestamp To Word - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / Php: Converting Timestamp To Word (1778 Views)

98% Of Web Visitors Leave Without Converting. This Is How To Get Them Back / Converting HTML To Joomla / Converting Html/webpage To Pdf With Php, Asp.net, Java And Others (2) (3) (4)

(1) (Reply) (Go Down)

Php: Converting Timestamp To Word by DualCore1: 4:19pm On Jul 28, 2011
About an hour ago I was searching for a php function that can convert a time stamp to words. That is 2011-07-28 04:54:54 to July 28, 2011 by 4:54 AM
Maybe I didn't search hard enough, I gave up and created the function myself. This should come in handy for devs if they dont already have something like it or better.

example usage
timestamp_to_word("2011-07-28 04:54:54", "date_and_time"wink //this gives you July 28, 2011 by 4:54 AM
timestamp_to_word("2011-07-28 04:54:54", "date_only"wink //this gives you July 28, 2011
timestamp_to_word("2011-07-28 04:54:54", "time_only"wink //this gives you 4:54 AM

===============================
//date to words
function timestamp_to_word($timestamp, $format){

$year = substr($timestamp, 0, 4);
$month = substr($timestamp, 5, 2);
$day = substr($timestamp, 8, 2);

$hour = substr($timestamp, 11, 2);
$minute = substr($timestamp, 14, 2);

//month in words
switch($month){
case "01";
$word_month = "January";
break;

case "02";
$word_month = "February";
break;

case "03";
$word_month = "March";
break;

case "04";
$word_month = "April";
break;

case "05";
$word_month = "May";
break;

case "06";
$word_month = "June";
break;

case "07";
$word_month = "July";
break;

case "08";
$word_month = "August";
break;

case "09";
$word_month = "September";
break;

case "10";
$word_month = "October";
break;

case "12";
$word_month = "Novermber";
break;

case "12";
$word_month = "December";
break;
}

switch($hour){
case "01";
$twelve_hour = "1";
$am_pm = "AM";
break;

case "02";
$twelve_hour = "2";
$am_pm = "AM";
break;

case "03";
$twelve_hour = "3";
$am_pm = "AM";
break;

case "04";
$twelve_hour = "4";
$am_pm = "AM";
break;

case "05";
$twelve_hour = "5";
$am_pm = "AM";
break;

case "06";
$twelve_hour = "6";
$am_pm = "AM";
break;

case "07";
$twelve_hour = "7";
$am_pm = "AM";
break;

case "08";
$twelve_hour = "8";
$am_pm = "AM";
break;

case "09";
$twelve_hour = "9";
$am_pm = "AM";
break;

case "10";
$twelve_hour = "10";
$am_pm = "AM";
break;

case "11";
$twelve_hour = "11";
$am_pm = "AM";
break;

case "12";
$twelve_hour = "12";
$am_pm = "PM";
break;

case "13";
$twelve_hour = "1";
$am_pm = "PM";
break;

case "14";
$twelve_hour = "2";
$am_pm = "PM";
break;

case "15";
$twelve_hour = "3";
$am_pm = "PM";
break;

case "16";
$twelve_hour = "4";
$am_pm = "PM";
break;

case "17";
$twelve_hour = "5";
$am_pm = "PM";
break;

case "18";
$twelve_hour = "6";
$am_pm = "PM";
break;

case "19";
$twelve_hour = "7";
$am_pm = "PM";
break;

case "20";
$twelve_hour = "8";
$am_pm = "PM";
break;

case "21";
$twelve_hour = "9";
$am_pm = "PM";
break;

case "22";
$twelve_hour = "10";
$am_pm = "PM";
break;

case "23";
$twelve_hour = "11";
$am_pm = "PM";
break;

case "00";
$twelve_hour = "12";
$am_pm = "AM";
break;
}


$date_and_time = $word_month." ".$day.", ".$year." by ".$twelve_hour.":".$minute." ".$am_pm;
$time_only = $twelve_hour.":".$minute." ".$am_pm;
$date_only = $word_month." ".$day.", ".$year;

if($format == "date_and_time"wink{
echo $date_and_time;
}
elseif($format == "date_only"wink{
echo $date_only;
}
elseif($format == "time_only"wink{
echo $time_only;
}
else{
echo "Invalid format";
}


}

============================
Re: Php: Converting Timestamp To Word by iwantto(m): 4:30pm On Jul 28, 2011
That is way too long. This is shorter:


function get_date($unix_time)
{
if(is_numeric($unix_time))
{
$read_in = date("F j, Y, g:i a", $unix_time);
return $read_in;
}
}
Re: Php: Converting Timestamp To Word by DualCore1: 4:34pm On Jul 28, 2011
LoL when I was writing it I knew there had to be a better way of doing it but I had no choice cuz I couldn't find any. Thanks.
Will try yours. cool
Re: Php: Converting Timestamp To Word by iwantto(m): 4:46pm On Jul 28, 2011
This should be the way to get it working. First convert the time stamp to unix time stamp and back to date. The function above should be modified to contain the Unix time stamp convertion.

function getTextDate($timestamp)
{
$unix_time = strtotime($timestamp);
if(is_numeric($unix_time))
{
$read_in = date("F j, Y, g:i a", $unix_time);
return $read_in;
}
}

echo getTextDate('2011-07-28 04:54:54');

Output should be:
==============================================
July 28, 2011, 4:54 am
Re: Php: Converting Timestamp To Word by Nobody: 7:27pm On Jul 28, 2011
Santa Claus! I thought it was ehm those codes that do - 5hrs 11 days et al
Re: Php: Converting Timestamp To Word by sayhi2ay(m): 7:40pm On Jul 28, 2011
always try to set a default value, and set your return outside your condition.

when dealing with numeric, try to test for string > 0 ,

is_numeric('0') would return false, what do your return then ?

function get_date($unix_time)
{
$read_in = null;

if( !empty($unix_time) && is_numeric($unix_time) && $unix_time > 0 ))
{
$read_in = date("F j, Y, g:i a", $unix_time);
}
return $read_in;
}
Re: Php: Converting Timestamp To Word by yawatide(f): 12:26pm On Jul 29, 2011
Rules of thumb (and I tweeted this yesterday to my followers):
1) When you write the same line of code more than once, know that it is time for a function
2) When your code goes very long, know that there is probably a better and more efficient way of writing it
Re: Php: Converting Timestamp To Word by DualCore1: 12:31pm On Jul 29, 2011
yes ma.
I have been trying to write the propose_to_yawa() seeing as I have to be proposing repeatedly before I get answer.

(1) (Reply)

Website For Sale At Cheap Price / Fast And Reliable VPS Needed / USA Non Verified Google Adsense Acct. For Sale @ GIVE AWAY Price

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 20
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.