PHP dates

This is a discussion on "PHP dates" within the PHP Forum section. This forum, and the thread "PHP dates are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Sep 5th, 2007, 07:49
simonb's Avatar
Blog Moderator

Join Date: Dec 2006
Location: Norwich
Posts: 677
Blog Entries: 4
Thanks: 4
Thanked 2 Times in 2 Posts
Send a message via Skype™ to simonb
PHP dates

In my Blog this code gets the date

PHP: Select all

<?php echo $row_entry['EntryDate']; ?>

But as you know i will come out like
2007-09-04 17:03:47

How do i reformat that
Last Blog Entry: Whats your Niche? (Jun 10th, 2008)
Reply With Quote

  #2 (permalink)  
Old Sep 5th, 2007, 08:01
Up'n'Coming Member
Join Date: Nov 2006
Location: Ipswich
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP dates

Ah, I had this problem. You've got your MySQL formatted to a DATETIME value presumably?

You need to convert it to a Unix Timestamp. This nifty function can do that for you...

Code: Select all
function convert_datetime($str) {

list($date, $time) = explode(' ', $str);
list($year, $month, $day) = explode('-', $date);
list($hour, $minute, $second) = explode(':', $time);

$timestamp = mktime($hour, $minute, $second, $month, $day, $year);

return $timestamp;
You can then reformat your date using the PHP date function.

That's how I'm doing on my new website. Others may have better ways.

Hope this helps.
Reply With Quote
  #3 (permalink)  
Old Sep 5th, 2007, 08:02
simonb's Avatar
Blog Moderator

Join Date: Dec 2006
Location: Norwich
Posts: 677
Blog Entries: 4
Thanks: 4
Thanked 2 Times in 2 Posts
Send a message via Skype™ to simonb
Re: PHP dates

Sorry were do i add that
Last Blog Entry: Whats your Niche? (Jun 10th, 2008)
Reply With Quote
  #4 (permalink)  
Old Sep 5th, 2007, 08:16
Up'n'Coming Member
Join Date: Nov 2006
Location: Ipswich
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP dates

You can declare your function anywhere, although you need to declare it before you call it.

I've just noticed I've missed out a closing parenthesis on that function.

Once you've declared you function you need something like...

Code: Select all
 
$myDate = $row_entry['EntryDate'];
$unixDate = convert_datetime($myDate);
$newDate = date("d m y", $unixDate);
echo $newDate;

I'm not one hundred percent sure that willl work as I'm at work at the moment. Work on that basis and with a little bit of Googling you should be alright.

Reply With Quote
  #5 (permalink)  
Old Sep 5th, 2007, 08:38
simonb's Avatar
Blog Moderator

Join Date: Dec 2006
Location: Norwich
Posts: 677
Blog Entries: 4
Thanks: 4
Thanked 2 Times in 2 Posts
Send a message via Skype™ to simonb
Re: PHP dates

PHP: Select all

<?php include('./Connections/noloafing.php'); ?>
<?php
$maxRows_LastArticals 
6;
$pageNum_LastArticals 0;
if (isset(
$_GET['pageNum_LastArticals'])) {
  
$pageNum_LastArticals $_GET['pageNum_LastArticals'];
}
$startRow_LastArticals $pageNum_LastArticals $maxRows_LastArticals;

mysql_select_db($database_noloafing$noloafing);
$query_LastArticals "SELECT EntryID, EntryName FROM entrys ORDER BY EntryID DESC";
$query_limit_LastArticals sprintf("%s LIMIT %d, %d"$query_LastArticals$startRow_LastArticals$maxRows_LastArticals);
$LastArticals mysql_query($query_limit_LastArticals$noloafing) or die(mysql_error());
$row_LastArticals mysql_fetch_assoc($LastArticals);

if (isset(
$_GET['totalRows_LastArticals'])) {
  
$totalRows_LastArticals $_GET['totalRows_LastArticals'];
} else {
  
$all_LastArticals mysql_query($query_LastArticals);
  
$totalRows_LastArticals mysql_num_rows($all_LastArticals);
}
$totalPages_LastArticals ceil($totalRows_LastArticals/$maxRows_LastArticals)-1;

mysql_select_db($database_noloafing$noloafing);
$query_Recordset1 "SELECT * FROM entrys ORDER BY EntryID DESC";
$Recordset1 mysql_query($query_Recordset1$noloafing) or die(mysql_error());
$row_Recordset1 mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 mysql_num_rows($Recordset1);

mysql_select_db($database_noloafing$noloafing);
$query_entry "SELECT * FROM entrys ORDER BY EntryID DESC";
$entry mysql_query($query_entry$noloafing) or die(mysql_error());
$row_entry mysql_fetch_assoc($entry);
$totalRows_entry mysql_num_rows($entry);

function 
convert_datetime($str) {

list(
$date$time) = explode(' '$str);
list(
$year$month$day) = explode('-'$date);
list(
$hour$minute$second) = explode(':'$time);

$timestamp mktime($hour$minute$second$month$day$year);

return 
$timestamp;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>NoLoafing.co.uk - Blog</title>
    <meta http-equiv="Content-Language" content="English" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="author" content="Simon Bennett" />
    <meta name="Description" content="Noloafing.co.uk Blog" />
    <meta name="Keywords" content="Blog,Seo,Noloafing" />    
    <meta name="Robots" content="index,follow" />
    
    <link rel="stylesheet" type="text/css" href="style.css" media="screen" />
    <link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="rss/" />
</head>
<body>
<div id="top">
    <div class="content">
            <div id="menu">
                <ul>    
                    <li><a class="current" href="/" title="Home"><span>HOME</span></a></li>
                    <li><a href="#" title="Articles"><span>ARTICLES</span></a></li>
                    <li><a href="#" title="Contact"><span>CONTACT</span></a></li>
                </ul>
            </div>
            <h1><a href="/" title="">NoLoafing.co.uk</a></h1>
            <h2>Bennett Talks About Web related issues. Also trying to learn the art of SEO</h2>
    </div>
</div>

    
<div class="content">
    <div id="main">
        <div class="right_side">
          <div class="pad">
                <h3>Thanks you visiting!</h3>
              <p>
                Noloafing was created on the 4 of September 2007! <br />
                Please vote For Me <a rel="nofollow" href="http://www.blogtoplist.com/vote.php?u=17161" >Here
</a> 
  <br /><a  href="http://www.blogtoplist.com/marketing-seo/" title="Marketing / SEO blogs">
<img src="http://www.blogtoplist.com/tracker.php?u=17161" alt="Marketing / SEO blogs" /></a>


                <br />
            </p>
                <h3>Last Articles:</h3>
                    <ul>
                      <?php do { ?>
                      <li><a href="<?php echo $row_LastArticals['EntryID']; ?>" title="<?php echo $row_LastArticals['EntryName']; ?>"><?php echo $row_LastArticals['EntryName']; ?></a></li>
                        <?php } while ($row_LastArticals mysql_fetch_assoc($LastArticals)); ?>
                    </ul>
        <p>        
</p>
                    
          </div>
        </div>
        
        
        <div class="right_side">
            <div class="pad">
              <?php include('./includes/categories.php'); ?>
              <?php include('./includes/links.php'); ?>
                    
                <h3>4 Sale </h3>
                <p>This site is up for sale. Please use the contact form if you what to talk details</p>
                
          </div>
        </div>
<div id="left_side">
      <div class="mpart">
                <h2><?php echo $row_entry['EntryName']; ?></h2>
                <h3> <?php$myDate $row_entry['EntryDate'];
$unixDate convert_datetime($myDate);
$newDate date("d m y"$unixDate);
echo 
$newDate?> in <a href="categorie.php?Cat=<?php echo $row_entry['EntryCat']; ?>" title=""><?php echo $row_entry['EntryCat']; ?></a></h3>
                <p><?php echo $row_entry['EntryContent']; ?></p>
                <div class="date"><a href="#" title="#">Comments (5)</a></div>
  </div>
</div>
    </div>
    <div id="footer">
        <div class="right">&copy; Copyright 2007, NoLoafing.co.uk </div>
            <a href="/rss/" title="RSS Feed">RSS Feed</a> - <a href="http://validator.w3.org/check?uri=referer" title="Validate">XHTML</a> - <a href="http://jigsaw.w3.org/css-validator/check/referer" title="Validate">CSS</a>
    </div>
</div>
</body>
</html>
<?php
mysql_free_result
($LastArticals);

mysql_free_result($Recordset1);

mysql_free_result($entry);
?>
is giving me
Quote:
Parse error: syntax error, unexpected $end in D:\Program Files\wamp\www\index.php on line 139
Last Blog Entry: Whats your Niche? (Jun 10th, 2008)
Reply With Quote
  #6 (permalink)  
Old Sep 5th, 2007, 08:47
Up'n'Coming Member
Join Date: Nov 2006
Location: Ipswich
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP dates

You need the curly bracket at the end of your convert_datetime function!
Reply With Quote
  #7 (permalink)  
Old Sep 5th, 2007, 09:03
simonb's Avatar
Blog Moderator

Join Date: Dec 2006
Location: Norwich
Posts: 677
Blog Entries: 4
Thanks: 4
Thanked 2 Times in 2 Posts
Send a message via Skype™ to simonb
Re: PHP dates

i get
01/01/70 : 01:01:00


now how would i get rid of the wasted 0's
Last Blog Entry: Whats your Niche? (Jun 10th, 2008)
Reply With Quote
  #8 (permalink)  
Old Sep 5th, 2007, 09:54
Up'n'Coming Member
Join Date: Feb 2006
Location: London
Age: 25
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP dates

There's a simlpler way to do this:
PHP: Select all

<?php echo date('d/m/y'strtotime($row_entry['EntryDate'])); ?>

This would output, for example, something like this:
30/12/07

have a look at 'Paramaters' to see how to format the date:
http://uk2.php.net/manual/en/function.date.php
Reply With Quote
  #9 (permalink)  
Old Sep 5th, 2007, 10:06
Up'n'Coming Member
Join Date: Nov 2006
Location: Ipswich
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP dates

If only I'd found the "strtotime" function last week when I spent ages to trying to find out a way of doing this! Cheers Jim!

Alas Simon, Jim's way is much easier. Although if you're getting a 1/1/1970 date then your date would appear to have a value of 0.
Reply With Quote
  #10 (permalink)  
Old Sep 5th, 2007, 10:12
Up'n'Coming Member
Join Date: Feb 2006
Location: London
Age: 25
Posts: 98
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP dates

Quote:
Originally Posted by ChrisTheSoul View Post
If only I'd found the "strtotime" function last week
lol, I feel your pain.
Reply With Quote
  #11 (permalink)  
Old Sep 5th, 2007, 11:01
simonb's Avatar
Blog Moderator

Join Date: Dec 2006
Location: Norwich
Posts: 677
Blog Entries: 4
Thanks: 4
Thanked 2 Times in 2 Posts
Send a message via Skype™ to simonb
Re: PHP dates

thanks that is much easyer
Last Blog Entry: Whats your Niche? (Jun 10th, 2008)
Reply With Quote
  #12 (permalink)  
Old Sep 5th, 2007, 14:30
c010depunkk's Avatar
SuperMember

SuperMember
Join Date: Apr 2007
Location: Willich, Germany
Age: 20
Posts: 593
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to c010depunkk
Re: PHP dates

Here's another solution using preg_match:
PHP: Select all

<?php
// input format (YYYY *deliminator* [M]M *deliminator* [D]D)
if(preg_match('/^([0-9]{4})([-_ \/\.])(0?[1-9]|1[012])\2(3[01]|[12][0-9]|0?[1-9])$/',$t,$r)) {
    
$year=$r[3];
    
$month=$r[3];
    
$day=$r[4];
} else {
    echo(
"Invalid date format");
}
?>
Reply With Quote
Reply

Tags
date, php

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Days between 2 Dates birdy247 JavaScript Forum 1 Apr 19th, 2007 19:55
Querying Dates Steve Mellor Databases 1 Apr 13th, 2007 14:59
printing out dates riotman Classic ASP 0 May 23rd, 2006 20:47
dates in SQL spinal007 Databases 3 Nov 10th, 2004 09:32
dates benbacardi Classic ASP 2 Sep 14th, 2004 16:18


All times are GMT. The time now is 20:57.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43