Solved

date format

Posted on 2011-02-18
16
279 Views
Last Modified: 2012-05-11
I am printing dates from a database as below:

$row_rs_tax_diary['date']

Currently this prints as the following date format:

11/03/19 - i.e. Year, Month, Date

I would like it to be converted to print as

Sat 19th March

How can I do this?
0
Comment
Question by:petewinter
  • 5
  • 5
  • 4
  • +2
16 Comments
 
LVL 19

Expert Comment

by:Bardobrave
ID: 34925019
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 34925023
try like this,

$date = $row_rs_tax_diary['date'];

$formatted = date('M. d, Y', strtotime($date));      
0
 

Author Comment

by:petewinter
ID: 34925123
logudotcom - Thanks but the result was incorrect.

The result was

Nov. 03, 2019

I want it to be

Sat 19th March
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 34925140
try this,
<?php
$date = '11/03/19';//$row_rs_tax_diary['date']

$formatted = date('D jS F', strtotime($date));      
// Sat 19th March

echo $formatted;

?>

Open in new window

0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 34925151
i think you need pass the correct date format ...so that it displays well
0
 
LVL 36

Accepted Solution

by:
Loganathan Natarajan earned 275 total points
ID: 34925154
this shows what you want,


$date = '2011/03/19';//$row_rs_tax_diary['date']


$formatted = date('D jS F', strtotime($date));      
// Sat 19th March

echo $formatted;
0
 
LVL 4

Expert Comment

by:andreizz
ID: 34925157
Use this string instead of:
$row_rs_tax_diary['date'] - this must be a timestamp

date("D jS F",$row_rs_tax_diary['date']);
0
 
LVL 36

Expert Comment

by:Loganathan Natarajan
ID: 34925160
you need to pass the year as "yyyy-mm-dd" ... you will get exact result
0
 

Author Comment

by:petewinter
ID: 34925205
logudotcom - Thanks, but the result of $row_rs_tax_diary['date'] is 11/03/19 not 2011/03/19 as you have used. This makes the result Sun 3rd November not Sat 19th March.

How can I fix this?
0
 
LVL 4

Expert Comment

by:andreizz
ID: 34925212
since this is the 21 century and will be for a long time, do something like this:

$new_date="20"+$row_rs_tax_diary['date'];

then you will have the format used by log
0
 
LVL 4

Assisted Solution

by:andreizz
andreizz earned 150 total points
ID: 34925219
sorry i used + from javascript :P
this:


$date = "20".$row_rs_tax_diary['date'];
echo date('D jS F', strtotime($date));      
// Sat 19th March
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 75 total points
ID: 34925228
Please read this article.  It will tell you most of what you need to know about using DATETIME fields in PHP and MySQL:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

For the requested format, Sat 19th March the date() pattern would be something like date('D jS F');
http://us2.php.net/manual/en/function.date.php

Best of luck with your project, ~Ray
0
 

Author Comment

by:petewinter
ID: 34925230
andreizz - Thanks I understand this the simplest solution and will not cause a problem for 89 years which I'm sure will be more than enough for my website life time, but if we were going to do this without any possible future bugs how would li be done?
0
 
LVL 4

Assisted Solution

by:andreizz
andreizz earned 150 total points
ID: 34925288
$date='11/03/19';
	$a=date("Y");
	$date=$a[0].$a[1].$date;
	echo date('D jS F', strtotime($date)); 

Open in new window


There you go :) this will work even when we will travel to other planets :D
0
 

Author Closing Comment

by:petewinter
ID: 34925347
Many thanks all.

21st Century here we come!!! :)
0
 

Author Comment

by:petewinter
ID: 34925353
or should I say 22nd Century here we come! :)
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question