PHP rename with date stamp - php

we want to make a script that renames the file name with a date stamp, we're new to PHP so this is what we have now.
<?php rename("test.txt", "tes3.txt"); ?>
we tried some with things like this below but we don't get it to work.
$date = new DateTime();
echo $date->format('Y-m-d H:i:sP') . "\n";
I hope you guys have a answer how we can do this.
Thanks

All you have to do is use the textual output (string) produced by $date->format(...) and inject the answer in the rename call.
$date = new DateTime();
rename("test.txt", "test" . $date->format('Y-m-d H:i:sP') . ".txt");
But it is advisable to not use any spaces in filenames, especially if you want the files to be accessible through a server/website/...

Related

create altered time record based on existing value

How do you take an existing 'date and time' value and convert it to the same date but a specified time?
For example, $time="2017-09-01 13:18:00" -> how to do you convert to "2017-09-01 23:59:59"? It must keep the date but change the time to 23:59:59.
You can do it like this by explode and simple concatenation
<?php
$time="2017-09-01 13:18:00";
$date = explode(" ", $time)[0];
echo $date." 23:59:59";
?>
Live demo : https://eval.in/853822
Update
I think you need this
<?php
$date = new DateTime('2017-09-01 13:18:00');
$date->setTime(23, 59,59);
echo $date->format('Y-m-d H:i:s') . "\n";
?>
Live demo : https://eval.in/853857
What about this?
$date = new DateTime('2017-09-01 13:18:00');
$date->add(new DateInterval('PT10H30S'));

PHP add time to date

I have a php time "09:00" that I want to set as the time for a date.
$date="2016-08-21 00:00:00.000000";
$time="09:00";
So ideally:
$datetime=$date+$time;//<----------what is the function for this
echo $datetime;
gives "2016-08-21 09:00:00.000000"
or
$date="2016-08-21 00:00:00.000000";
$time="17:30";
then
$datetime=$date+$time;//<----------what is the function for this
echo $datetime;
gives "2016-08-21 17:30:00.000000".
A seemingly simple requirement I cannot find a solution to.
try this :
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('PT10H30S'));
echo $date->format('Y-m-d H:i:s') . "\n";
?>
Pending Mark adding an answer this was what I had but it seems clumsy - I hoped there was a better way:
$timebits = explode(":", $timepart);
$now->setTime($timebits[0], $timebits[1], $timebits[2]);

adding days with a date in php

In my project i have to get the expiry date of a package from the 'date of purchasing' the package and the 'validity in days' of the package.I got a code from the php.net as follows
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P10D'));
echo $date->format('Y-m-d') . "\n";
?>
Now suppose the date of purchase is like
2013-01-17
and the validity in days is
180
Now if i use the php.net example as follows
<?php
$date = new DateTime('2013-01-17');
$date->add(new DateInterval('P180D'));
echo $date->format('Y-m-d') . "\n";
?>
Will i get the output as 2013-04-17?
I need the output like
yyyy-mm-dd
I cant run the program in my personal computer but i have to carry on the codding of the project so i'm here.
<?php
$date = new DateTime('2013-01-17');
$date->add(new DateInterval('P180D'));
echo $date->format('Y-m-d') . "\n";
?>
//output
//2013-07-16 and it is in yyyy-mm-dd format
working example http://codepad.viper-7.com/gAtT2D
and if you can't run the program in your PC, use online debugger shell.

Properly Update A Timestamp's Timezone

I'm using the LastFM API to extract a user's recently listened-to tracks (http://www.last.fm/api/show/user.getRecentTracks) and am struggling to shift the timestamp to match my preferred timezone.
I've used date_default_timezone_set at the beginning of the code, but that seems to be ignored when I use strtotime. I'm using strtotime so that I can reformat the styling of the date as Last.FM provides it.
I've figured out how to manually offset to the correct time, via $date - 14400, but I'd like to understand what I'm missing and make the adjustment in the correct way.
Code follows. Greatly appreciate any assistance.
<?php date_default_timezone_set('America/New York'); ?>
<?php $xml = simplexml_load_file("http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=rj&api_key=b25b959554ed76058ac220b7b2e0a026");
echo '<ul>';
foreach($xml->recenttracks->track as $track) {
$title = $track->name;
$date = $track->date;
$date = strtotime($date);
$date = date("F jS, g:i a e", $date);
$string = '<li>'.$title.' - '.$date.'</li>';
echo $string;}
echo '</ul>';
?>
The problem is that the default timezone you set is used for both the incoming and outgoing translation.
To solve, use PHP timezone functions, and flip it about when reading / writing the time:
$oDateTime = new DateTime($track->date, new DateTimeZone(UTC'));
echo $oDateTime->format('F jS, g:i a e') . "\n"; // For debug: Will give the same back at you.
$oDateTime->setTimezone(new DateTimeZone('America/New York'));
$date = $oDateTime->format('Y-m-d H:i:sP') . "\n"; // Will give the converted date
(You can also do it with date_timesone_set, but this looks neater).

PHP/MySQL Timezone Clarification

I'm currently having an issue wrapping my brain around the notion of converting my MySQL time to a specific timezone, depending on the user's settings.
All of my MySQL times are stored in UTC time, in the following format:
2009-11-08 17:06:40
Once I query the time, I'm not quite sure how to convert it to the appropriate timezone using PHP.
Thus, in the example above, I'd like to display:
2009-11-08 09:06:40
Here's what I currently have (which probably needs to be fixed):
$sql = 'SELECT date FROM mytable';
require("connection.php");
$result = mysql_db_query($DBname,$sql,$link);
while($row = mysql_fetch_assoc($result)) {
$dt_obj = new DateTime($row['date']);
$dt_obj->setTimezone(new DateTimeZone('PST'));
echo $dt_obj;
echo "<br>";
}
First off, I get the following error:
Catchable fatal error: Object of class DateTime could not be converted to string
Secondly, I am confused as to whethere I'm setting it up properly to display the time in the correct timezone anyway (in this case, PST).
Any suggestions on how to do this would be greatly appreciated. Thanks!
UPDATE:
I took GZipp's advice, and modified the code to look like:
$dt_obj->setTimezone(new DateTimeZone('America/Los_Angeles'));
echo $dt_obj->format('Y-m-d H:i:s');
However, it's displaying my time (using the example from above) as:
2009-11-08 15:06:40
Any ideas on what would be causing this?
I think this is what you're after;
$dt_obj = new DateTime($row['date']);
$dt_obj->setTimezone(new DateTimeZone('America/Los_Angeles'));
echo $dt_obj->format('Y-m-d H:i:s');
Use the List of Supported Timezones.
Update to edited question: To ensure that PHP sees the time from the database as UTC time, do something like this:
$row['time'] = '2009-11-08 09:06:40';
$dt_obj = new DateTime($row['time'], new DateTimeZone('UTC'));
echo 'UTC: ' . $dt_obj->format('Y-m-d H:i:s') . '<br />'; // UTC: 2009-11-08 09:06:40
$dt_obj->setTimezone(new DateTimeZone('America/Los_Angeles'));
echo 'Los Angeles: ' . $dt_obj->format('Y-m-d H:i:s'); // Los Angeles: 2009-11-08 01:06:40
I may be wrong, but it looks like it's complaining about your echo $dt_obj; statement. You might try echoing the result of DateTime::format();
EDIT: For your new question I would guess that your default format is set to PST already, so when the new date is created it's create with that timezone, thereby setting the timezone changes nothing. You might check and see if that's the case. You also might look at date_default_timezone_set('<tz>');

Categories