Jump to content

PHP Timestamp


Go to solution Solved by AbraCadaver,

Recommended Posts

Is there a way to add a timestamp when finished uploading to server?
 
For example:
An image named Testing.jpg is uploaded to the server.
I want it to automatically be renamed to
 
2013_08_22-15:09:27-Testing.jpg
(Year_Month_Day-Hour:Minute:Second-FILENAME.EXTENSION)
 
Maybe the following code could help you while fixing my script.
 
 
$date = new DateTime();
echo 
$date->format('U = Y_m_d-H:i:s')
 
 
 
My current code:

<?php
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
  $filename = basename($_FILES['uploaded_file']['name']);
  $ext = substr($filename, strrpos($filename, '.') + 1);
  if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") && 
    ($_FILES["uploaded_file"]["size"] < 350000000)) {
      $newname = dirname(__FILE__).'/Uploads/'.$filename;
      if (!file_exists($newname)) {
        if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
           echo "It's done! The file has been saved as: ".$newname;
        } else {
           echo "Error: A problem occurred during file upload!";
        }
      } else {
         echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
      }
  } else {
     echo "Error: Only .jpg images under 350000Kb are accepted for upload";
  }
} else {
 echo "Error: No file uploaded";
}
?>

 
PS: I have only recently started to learn PHP coding (About 3 days ago)
Link to comment
https://forums.phpfreaks.com/topic/281479-php-timestamp/
Share on other sites

$newname = dirname(__FILE__) . '/Uploads/' . date('Y_m_d-H:i:s') . '-' . $filename;

 

Thanks so much!

 

But may I ask why in the tutorial it says 

$date = new DateTime();

echo $date->format('U = Y_m_d-H:i:s')

 

instead of

 

echo date('Y_m_d-H:i:s')?

Link to comment
https://forums.phpfreaks.com/topic/281479-php-timestamp/#findComment-1446383
Share on other sites

  • Solution

Thanks so much!

 

But may I ask why in the tutorial it says 

$date = new DateTime();

echo $date->format('U = Y_m_d-H:i:s')

 

instead of

 

echo date('Y_m_d-H:i:s')?

 

date() is a date formatting function.  The DateTime class can format a date / time but it has many more features to manipulate dates and times etc.  Since all you needed was to format todays date, I didn't see any need to create an object.

Edited by AbraCadaver
Link to comment
https://forums.phpfreaks.com/topic/281479-php-timestamp/#findComment-1446384
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.