Convert numerical date into Datetime object - php

Just wondered what the best way to convert the date in the following format:
02072014 (2nd July 2014) into the \DateTime object? I've tried:
$date = DateTime::createFromFormat("jMY", $digits);
But that didn't seem to work. I'm using that date format because it's being entered on a phone keypad.
Cheers,
Ewan

The function definition of createFromFormat()
public static DateTime DateTime::createFromFormat ( string $format , string $time [, DateTimeZone $timezone ] )
So you need to do as
$digits = '02072014';
$date = DateTime::createFromFormat('dmY',$digits);
echo $date->format("j M Y");
Set the data in your desired format.
$date->format("j M Y");

You are using the wrong format specifiers. From the documentation, j is used for day without leading zeroes, and m is for a month that is spelled out in English. Also, you should pass a DateTimeZone if you do not want PHP to guess.
<?php
$digits = "02072014";
$date = DateTime::createFromFormat("dmY", $digits, new DateTimeZone('UTC'));
echo $date->format('Y-m-d');
?>

Use the right format that the passed in string should be:
$date = DateTime::createFromFormat( "dmY", $digits );

Related

Convert ordered string to mysql Datetime

I have an API that returns a date string like this : 190416102906
Which means : 19 April 2016 at 10:29:06.
I want to store it in my Mysql Datetime but I don't know how to format it. It need to go from 190416102906 to 2016-04-19 10:29:06.
I've tried to use something like this but I probably didn't understand how it works :
$date_reponse = '190416102906';
$date = DateTime::createFromFormat("dmYHis",$date_reponse);
$newDate = date_format($date, 'Y-m-d H:i:s');
Because your format was incorrect : "dmYHis"
Y = Year format with 4 digit.
use y year with 2 digit (dmyHis)
$date = DateTime::createFromFormat("dmyHis",$date_reponse);
You just got the format the wrong way round and you need a lower case y for a 2 char year
$date_reponse = '190416102906';
$date = DateTime::createFromFormat("dmyHis",$date_reponse);
$newDate = date_format($date, 'Y-m-d H:i:s');

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));

PHP - convert date from non standard format

I am parsing a feed which returns the date in form:
20150129180000
which should be [yyyy][mm][dd][hours][minutes][seconds]
I tried formatting this with the php date() function:
date('Y-m-d H:i:s', $event['start-dt'])
and got this:
640502-03-05 12:33:20
which is obviously not right. Is there a smooth way of converting this, or do I simply need to splice the string manually?
Doc
$date = DateTime::createFromFormat('YmdHis', '20150129180000');
echo $date->format('Y-m-d H:i:s');
You can use preg_replace to format the string
$date_str = preg_replace("/^(\d){4}(\d){2}(\d){2}(\d){2}(\d){2}(\d){2}/","$1-$2-$3 $4:$5:$6",$event["start-dt"]);
date('Y-m-d H:i:s', strtotime($date_str));
Or use php's DateTime object
$date = new DateTime($date_str);
Or better yet, use the DataTime createFromFormat method
$format = "YmdGis";
$dateObj = DateTime::createFromFormat ( $format, $event['start-dt'] );
Then you can use getTimestamp to retrieve the timestamp for calculations, if you need it.
The format is somewhat confusing compared to date until you are used to it, but this corresponds to your date string:
$date = DateTime::createFromFormat('YmdGis', '20150129180000');
echo $date->format('Y-m-d H:i:s');

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

problem with date()

I am using the date() function to display a timestamp from my databse.
$date = date( 'F jS', $news_items['date']);
I know the $news_items['date']; as they return in a YYYY-MM-DD 00:00:00 format.
But after the function call $date dispays as December 31st for all values.
$date = date('F jS', strtotime($news_items['date']));
Try that :)
That's because date() wants a timestamp and not a string. You're better of with something like this;
$dt = date_create($news_items['date']);
$date = date_format($dt, 'F jS');
Or in the object oriented way;
$dt = new DateTime($news_items['date']);
$date = $dt->format('F jS');
The correct syntax for date function is given in PHP manual as:
string date ( string $format [, int $timestamp ] )
As you can see the second argument is expected to be an integer, but you are feeding the function with a string.

Categories