Jump to content

[SOLVED] Formatting a date from a variable?


denhamd2

Recommended Posts

hi,

 

I have the date in the format 01012007 stored in the variable $mydate

 

is there any way I can echo this in the format Monday, 1st January 2007?

 

 

I was told using mkdir get timestamp for this 01012007

 

Then use date function to change date format... I'm a bit of a newbie so would anyone know how to do this?

 

Thanks in advance

 

 

Link to comment
Share on other sites

um, is is a string, or a timestamp or what???

 

 

if string, then look at the string sormatting functions, look for one that will slice the string at every "x" characters.  then simply format from the array output,

 

if timestamp, then go $new_date = date("FORMATING STRING HERE")

um, look at "date formatting" in tutorials

 

good luck

Link to comment
Share on other sites

i think you should go for using substr() and the date() and mktime() maybe to get the info about named months like january, february etc...

 

here's a something quick i made that you can use as a guideline..

<?php

$string = "01012007";
$month = substr($string, 0, 2);
$day = substr($string, 2, 2);
$year = substr($string, 4, 7);

?>

 

to find out more go to:

http://us2.php.net/manual/en/function.substr.php

http://us2.php.net/manual/en/function.date.php

http://us2.php.net/manual/en/function.mktime.phpr

Link to comment
Share on other sites

<?php

 

$string = "01012007";

$month = substr($string, 0, 2);

$day = substr($string, 2, 2);

$year = substr($string, 4, 7);

 

            $timestamp = mktime(0,0,0,$month,$day,$year);

 

$formateddate = date("l, jS Y" ,$timestamp); 

 

?>

 

Link to comment
Share on other sites

<?php

 

  $string = "01012007";

  $month = substr($string, 0, 2);

  $day = substr($string, 2, 2);

  $year = substr($string, 4, 7);

 

            $timestamp = mktime(0,0,0,$month,$day,$year);

 

$formateddate = date("l, jS Y" ,$timestamp); 

 

?>

 

Link to comment
Share on other sites

you're forgetting he wanted to print out the full name month to... ;D

 

<?php

   $string = "01012007";
   $month = substr($string, 0, 2);
   $day = substr($string, 2, 2);
   $year = substr($string, 4, 7);

             $timestamp = mktime(0,0,0,$month,$day,$year);

$formateddate = date("l, F jS Y" ,$timestamp); 
echo $formateddate;

?>

 

prints out:

Monday, January 1st 2007
Link to comment
Share on other sites

yeah... i was just so happy... someone actually asked about something that I knew and didnt have to look up first..hehe... got a lil carried away... sorry jitesh ;D hope i didnt offend you ???

 

and denhamd2 .. dont forget to mark as SOLVED

Link to comment
Share on other sites

bit of a problem, its taking 25042007 and formatting it as Sunday, January 4th 2009... any ideas as to why this could be?

 

EDIT: sorted this now - i had to change month, day, year to day, month, year :) [european style date formatting]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.