Hi everyone, am trying to display current date/today's date in php but the output is as below.
Please advise. Thanks.

<?php
$Currentdate2 = date('d-m-y');
echo $Currentdate2;
?>

Output:
Current date: 2027-10-15

Recommended Answers

All 12 Replies

The output is 27-10-15, which is exactly what you've requested (day - month - year).
What date format where you trying to get?

Hi, thanks for your reply. The output date 2027-10-15,it is not 27-10-15. Please advise. Thanks.

Hmmm...
What does this return for you?

<?php
$Currentdate2 = date('D-m-y');
echo $Currentdate2;
?>

Hi, it echoed Tue-10-15. Please advise. Thanks.

There is nothing in PHP's date function that will return a day with '20' prepended to the front of it, so it must be something outside of that.

Try this

<?php
print_r(getdate());
?>

That will return an array with date information, it should show 'mday' as 27.

Hi, please find the output for the getdate() above :

Array ( [seconds] => 53 [minutes] => 4 [hours] => 8 [mday] => 27 [wday] => 2 [mon] => 10 [year] => 2015 [yday] => 299 [weekday] => Tuesday [month] => October [0] => 1445933093 )

Appreciate for your advise. Thanks.

Hi, here's the code:

 <?php
 date_default_timezone_set("Asia/Bangkok"); // set your timezone.
echo date("d-m-y");
?>

Here's a list of timezones you can set: http://php.net/manual/en/timezones.php

Hi, have tried that to set the timezone but it still echo 2027-10-15. Wonder what's the problem. Thanks.

Hi, sure there is no % in front of 20?

Can you show us more details of the script in which you create the date? Have you tried the above in a separated script: a simple <?php echo date('d-m-Y'); ?> file, in this case do you get the same results?

As pixelsoul and cereal wrote the "20" in front isn't coming from that part of the code. It comes either from a part above or by html . Follow cereal advice to create a file just printing the result of the date(“d-m-Y”) and then return in your code printing a break line <br/> above printing your variable. There you will see that this isn't from this part of the code. A debugging tool could help you , but if you don't like them for some reason you must learn to debug even with var_dump or so.

Hi everyone, when i tried to put the current date in a separate file, the date is correct 28-10-15. But when i tried the coding in the form as below, it still display 2028-10-15. Appreciate for your advise. Thanks.

       <form name="progress" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <?php
        error_reporting(E_ALL ^ E_NOTICE);
        $cn=mysql_connect("localhost","user","") or die(mysql_error());
        mysql_select_db("pq",$cn) or die(mysql_error());

        $Ipid=$row['Ipid'];
        $Progressid=$row['Progressid'];
        date_default_timezone_set("Asia/Bangkok"); // set your timezone.
        $Currentdate2 = date('d-m-y');

        if(isset($_POST['Notify2'])){
        date_default_timezone_set("Asia/Bangkok"); // set your timezone.
        $Currentdate2 = date('d-m-y');

        $sql=mysql_query("Update improvement_plan set Currentdate2='$Currentdate2' where Progressid='".$Progressid."'");
        $result = mysql_query($sql);
        ?>
            <table width="850" border="1">
              <tr align="center">
               <th width="70" scope="col">Ipid</th>   
                <th width="144" scope="col">Date</th>  
            <input type="submit" name="Notify2" value="Notify"/> 
             </tr>   
        <?php 

            $disable = '';
            $i = 0;
            $d = 0; 

        $con = mysql_connect("localhost","user","");
        if (!$con)
        {
        die('Could not connect: ' . mysql_error());
        }
        mysql_select_db("pq", $con);    

        $sql = "SELECT * FROM improvement_plan where  Progressid='".$Progressid."'";
        $res_list = mysql_query($sql);

        while($row_list = mysql_fetch_array($res_list)){
         //   $i++;     
        ?>  
                 <td align="center"><div name="Ipid" id="Ipid<?php echo $i; ?>"><?php if($row_list['Ipid'] != '0000-00-00'){ echo ($row_list['Ipid']); }else{ echo '-'; } ?></div></td>
                 <td align="center"><div name="Currentdate2" id="Currentdate2<?php echo $i; ?>"><?php if($row_list['Currentdate2'] != '00-00-00'){ echo ($row_list['Currentdate2']); }else{ echo '-'; } ?></div></td>  
                 <td align="center"><div name="Progressid" id="Progressid<?php echo $i; ?>"><?php if($row_list['Progressid'] != '0000-00-00'){ echo ($row_list['Progressid']); }else{ echo '-'; } ?></div></td> 
                 </tr> 
        <?php
        } 
        if($i == 0){
        ?>
            <tr><td colspan="7">No data.</td></tr> 
        <?php
           $disable = 'disabled="disabled"';
        }
        ?>
        <?php
        if($row['Status2'] == 'buka'){ }
        ?> 
            <br>
            </table> 
            <br>
            <?php
            mysql_close($con);
        ?>
        </form>  
Member Avatar for diafol

WHat form are you talking about? There is no form, just a few divs.
You are using deprecated mysql_* functions - use PDO/mysqli.
This way of coding is going to end in tears - all the mish-mashing of PHP and HTML - try to separate these out as far as possible. THis is not maintainable.

Where is $Ipid=$row['Ipid']; coming from? $row doesn't exist - or are you not showing your code?

You never mentioned in your previous posts that you were storing this data in a DB and then pulling it out again. DB tables DATE datatype is YYYY-MM-DD, so if you tried to store 28-10-15 it would think you were talking about 2028-10-15, NOT 28-10-2015.

ALways use UNIX format dates, so:

$Currentdate2 = date('Y-m-d');

Store the dates like these and you can reformat them to display in a HTML table.

commented: That's it. +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.