Solved

date format

Posted on 2011-02-18
16
277 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

911 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now