php function for linux time conversion - php

I'm new to PHP and I need a PHP function that converts Linux times like 1396310400 from $variable and returns a human-readable date like 1. apr. 2014! (time of day not needed)? Any suggestions is much appreciated.

echo date('d/m/Y', $time_linux);
read here: http://php.net/manual/en/function.date.php to learn how to modify the result to show exactly what you want.

Thanx for your input.
This worked and returned a human readable date into my wpallimport run - at first I just did not understand the syntax:
function overtagelsesdato($time_linux) {
echo date('d/m/Y', $time_linux);
}
thank you OfirH!

Related

Javascript like timeformat in PHP

In Javascript to get the current time for the time stamp I'm using
JSON.stringify(new Date());
Which is giving me following output.
"2016-06-07T10:38:54.767Z"
In PHP how can I get this kind of output in datetime function.
I know this problem can be handled by date function of php. But what I'm looking for is exact argument to that date function so that I can get the output same as given in the question. Which is in UTC timezone. There are similar questions explaining date function but I'm not able to figure out exact arguments for my desired output.
In PHP I tried this
date_default_timezone_set('UTC');
echo date('c');
Which is giving me
2016-06-07T11:56:54+00:00
Thanks in advance.
Reading from the DateTime Class php.net documentation,
You can use $yourdate->format('c')
http://php.net/manual/en/datetime.format.php
See here for date format details :
http://php.net/manual/en/function.date.php

Strange date issue with PHP strtotime

I'm trying to store some dates in my datebase (MySQL). But I got a strange conversion error:
This is my piece of PHP code:
$fechanac=mysql_real_escape_string(($_POST["fechanac"]));
echo "<h1>{$fechanac}</h1>";
$fechanac=date('Y-m-d', strtotime(str_replace('/', '-', $fechanac)));
echo "<h1>{$fechanac}</h1>";exit();
See the following three example, trying with (01/01/1900, 01/01/1901 and 01/01/1902).
OUTPUT:
01/01/1900
1970-01-01
01/01/1901
1970-01-01
01/01/1902
1902-01-01
Somebody know why happens this? And how to fix it? I need to insert in my DB, possible dates of living persons. Thanks for reading.
Date stored!
$fechanac=mysql_real_escape_string(($_POST["fechanac"]));
echo "<h1>{$fechanac}</h1>";
$datearray=explode("/",$fechanac);
$fechanac="{$datearray[2]}-{$datearray[1]}-{$datearray[0]}";
I know it's and old post, but maybe this late answer can help others:
The DateTime class correctly manages dates older than 1970. So forget the use of date() and evolve to DateTime:
$fechanac=mysql_real_escape_string(($_POST["fechanac"]));
echo "<h1>{$fechanac}</h1>";
$fechanac=(new DateTime($fechanac))->format('Y-m-d');
echo "<h1>{$fechanac}</h1>";exit();

Formatting a date like 2013-11-01T08:50:43.305Z

Maybe a trivial question but I can't seem to figure this out. I need to format a date like 2013-11-01T08:50:43.305Z in PHP. I'm not sure how to add .305Z-part in the formatting. I've been looking at the docs (php's date-function documentation) but I must have overread it there... Any help is appreciated.
use this:
echo date("c");
For more reference see here: http://php.net/manual/en/function.date.php
Or you can use this format:
echo date("Y-m-dh:i:sz");
Here Z means the number of day in the current year.

Is it possible to manipulate the date this PHP code outputs?

<?php echo invoice_due_date($invoice); ?>
The above code outputs a date that is formatted as dd/mm/yyyy. I am looking for a way to take that date and then add or remove x amount of days from it and then print the result. I'm a novice when it comes to PHP so everything I have tried has failed. Thanks in advance!
Also, this code snippet is from a no longer supported project called "myclientbase" if it helps.
PHP5 has a nice class called DateTime.
You can initialize it from a string like this:
$date = DateTime::createFromFormat('d/m/Y', invoice_due_date($invoice));
Then, since PHP 5.3.0 this class has a method to add time amounts:
$date->add(new DateInterval('P10D'));
This adds 10 days to your date. See http://php.net/manual/en/datetime.add.php.
strtotime can translate the date to a time value (although you'd have to replace the slashes with dashes) and in the same operation add days:
strtotime(str_replace('/','-',invoice_due_date($invoice)) . ' + 1 day')
date can be used to format the date back to a suitable notation.
You can use the DateTime::add function. A full explanation and examples can be found at http://php.net/manual/en/datetime.add.php
$invoice->add(new DateInterval('P20D'));
This would add 20 days to the invoice date. You can simply run this before echoing out your $invoice variable.

convert one date format to another

I need help to convert a formatted date format as stated below:
How can I convert 2010-02-06 14:44:43 to dd/mm/yyyy
Thanks
Great place to start for this type of general information, php docs, specifically related to your date format question: http://us3.php.net/manual/en/function.date.php
-- Edit --
Answer with substr() forced me to do the work... using substr() to format a date is not the best option as PHP has built-in functions for that.
var_dump(date('d/m/Y', strtotime('2010-02-06 14:44:43')));
$date = new DateTime('2010-02-06 14:44:43');
echo $date->format('d/m/Y');
Output:-
06/02/2010
See the manual
Try this:
<?php
$time = strtotime('2010-02-06 14:44:43');
echo date('d/m/Y',$time);
?>

Categories