How to convert Unixtimestamp to date - php

I am using a array where they use unixtimestamp, like 1421241074.
Any idea how to convert it to date? I know that strtotime is date to unixtimestamp and that is exactly what I don't want.
I am trying something like this at the moment:
foreach ($response as $key => $value) { //this works
$timestamp = substr($value['timestamp'], 6, 13); //this works
$timestamp = ('m/d/Y H:i:s', $timestamp); // this supose to convert unixtimestamp to date. fail!
but this is not working, someone here with a idea?

You need the date() php function:
date('m/d/Y H:i:s', $timestamp);
In your code you forgot the date part of date()!

date('m/d/Y H:i:s', $timestamp);
Try to read about the timestamp function, in the documentation you should see about converting unix.

Related

Convert Time stamp to readable format

I need to conver timestamp to 2016-07-12 format. This is what I tried.
$selectedDate=date('m/d/Y H:i:s', '1465430400000');
I got 08/23/48407 00:00:00 I need to conver it to 2016-07-12 format.
Please Note: Here the format m/d/Y H:i:s isn't the matter. I'm getting wrong date is the problem
Any suggetion would be appricieated.
It looks like your timestamp is 1000x what date() expects, so try first dividing it by 1000 (and then, of course, use the right date format):
$selectedDate = date('Y-m-d', 1465430400000/1000);
You can convert Date in Any format:
<?php $date1 = strtotime($old_date);
echo $date = date("y-M-d", $date1); ?>
Complete list of format options

PHP date year format

I am having problems with dates in php- sometimes the date gets to us in d/m/y and other times its d/m/Y. I want to convert all dates to d/m/Y.
Working with my current dataset, how would I get 24/06/2015 from 24/06/15 using php?
So far I have tried :
$original_date = '24/06/15';
$new_date = date('d/m/Y', strtotime($original_date));
This brings back 01/01/1970
This is probably the most robust method:
$string = '24/06/15';
$date = DateTime::createFromFormat('d/m/y', $string) ?: DateTime::createFromFormat('d/m/Y', $string);
echo $date->format('d/m/Y');
createFromFormat returns false if you try to parse 24/06/2014 using the d/m/y format, so in that case you just retry with d/m/Y. You then get a DateTime object which you can format and output any way you like.
use the lowercase 'y'. See the PHP date manual.
$new_date = date('d/m/y', strtotime($original_date));
y = A two digit representation of a year
The problem is that the strtotime doesn't recognise the UK date format, so convert the format first then format the date.
Try this:
$original_date = "24/06/15";
list($date,$month,$year) = sscanf($original_date, "%d/%d/%d");
$date_convert = $year."-".$month."-".$date;
$new_date = date("d/m/Y", strtotime($date_convert));
echo $new_date;
Its wrong format of date you are using for strtotime.
Have a look at Date Formats
The correct code should have
$original_date = '15/06/24'; // Notice : its mm/dd/yy here
$new_date = date('d/m/Y', strtotime($original_date));

How to turn a string like "16/Sep/2014 08:34" to unix timestamp in PHP

Struggling without much success to turn strings like "16/Sep/2014 08:34" extracted from an array with explode command to unix timestamp like "2014-09-17 05:32:05" in PHP. Any help, please?
Edit: With #Erik's help I finally got the right result:
$date = DateTime::createFromFormat("d/M/Y H:i", $line[0]);
$date = $date->format('Y-M-d H:i');
$timestamp = strtotime($date);
You'll need to use DateTime::createFromFormat and then convert the resulting datetime to a timestamp by using $datetime->getTimestamp();
--
// this will create a generic PHP date object, which you can then manipulate into anything you want
$date = DateTime::createFromFormat( "d/M/Y H:i", "16/Sep/2014 08:34" );
// this will generate a unix timestamp (which is an integer)
$timestamp = $date->getTimestamp();
// this will generate the string you request in your question
$string = $date->format( "Y-m-d H:i:s" );
--
For more info on formatting dates, check out the PHP documentation: http://php.net/manual/en/datetime.createfromformat.php

Convert Time to Time Stamp PHP

I have a string :
$Value = 03/25/2014 10:15 AM
I would like to convert this to a mysql timestamp. When I use date('Y-m-d H:i:s', $Value) it does not work. Can someone please help me convert this? Thank you.
date needs a UNIX timestamp:
$Date = date('Y-m-d H:i:s', strtotime($Value));

date format function

Maybe is a dumb question, but I can't find the proper function:
I have a string date like 04/05/2012
How I can pass to this format: 2012-04-05 20:18:11 to insert in mySQL?
Combination of date and strtotime ...
$my_date = date('Y-m-d H:i:s',strtotime('04/05/2012'));
$dateTime = new DateTime($your_time_string);
echo $dateTime->format("Y-m-d H:i:s");
Time can not be 'made' on its own. If you're however inserting the current date-time setting, then the following code will be used:
$dt = date( "Y-m-d H:i:s", time() );
You may use dleiftah's statement, but the time in that case will always be 00:00:00

Categories