cakephp 3 + date convert from dd/mm/yy to yyyy-mm-dd - php

How can I convert date which is in dd/mm/yy format to sql insert date format.
I tried
$time = new \DateTime('d/m/y H:i', $this->request->data['date_from']);
pr($time);
getting error.
DateTime::__construct() expects parameter 2 to be DateTimeZone, string
given
When I try
DateTime::createFromFormat('d/m/y H:i', $str)->format('Y-m-d')
Error: Class 'App\Controller\DateTime' not found
How can I do that

this works for me $myDate = date('Y-m-d', strtotime($this->request->data['date_from']))

For Cakephp 3.2 and above:
use Cake\I18n\Date;
$date = new Date($this->request->data['date_from']);
echo $date->format('Y-m-d');
Reference: https://book.cakephp.org/3.0/en/core-libraries/time.html#dates

Related

Timestamp convert of US date format [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I have date in this format 'm-d-Y' (12-31-2017). I want to convert it into a timestamp. Normally, this works:
$timestamp = strtotime($date);
But if I am not mistaken, that works with 'd/m/Y' and not 'm/d/Y'.
What's the solution here?
You can use DateTime::createFromFormat() and then call getTimestamp() on the Object:
//first create DateTime Object
$datetime = DateTime::createFromFormat('m-d-Y', '12-31-2017');
//get timestamp from DateTime
echo $datetime->getTimestamp();
An another solution is the following:
//CONVERT the time string from the desired format
$dateTime=DateTime::createFromFormat('m/d/Y',$date);
echo "UNIX TIMESTAMP method 1".$date->format("U");
// false
echo "UNIX TIMESTAMP method 2".$date->getTimestamp();
You can change date according your format :
$date = '12/31/2017';
$timestamp = date('Y-m-d', strtotime($date));
$timestamp = date('d-m-Y', strtotime($date));
$timestamp = date('m-d-Y', strtotime($date));
if Timestamp
$timestamp = date('m-d-Y h:i:s', strtotime($date));
Try this code .
<?php
$d=strtotime("12/31/2017");
echo "Created date is " . date("m-d-Y h:i:sa", $d);
?>
Output
for more Information about date time format then read PHP Manual

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 convert ISO Date to DateTime in php?

How to convert the ISO date in mongodb:
ISODate("2015-11-20T10:00:09.809Z")
to php date ?
You should check MongoDate:
//build a MongoDate object from a string format
$mongoDate = new MongoDate( strtotime("2010-01-15 00:00:00") );
Once you have a MongoDate object (this is probably your case), you can convert it to a DateTime object this way:
//get a DateTime object
$phpDate = $mongoDate->toDateTime();
And finally convert it to the format you want:
$phpDate->format('M-d-Y');
$date = '2011-09-02T18:00:00';
$time = strtotime($date);
$fixed = date('l, F jS Y \a\t g:ia', $time);
when you extract ISODate("2015-11-20T10:00:09.809Z") from MongoDB
you will get two different timestamps for date=>sec and time=>usec
Example:
"created_at":{"sec":1592249762,"usec":53000}
Then you can add both sec and usec and you will get new timestamp.
$newTimeStamp = $created_at->sec+$created_at->usec;
echo date('Y-m-d H:i:s', $newTimeStamp);

select datetime with strtotime()

i want to select data which has a type datetime in mssql with php. I tried this code:
$date = strtotime($row['DateTime']);
echo date('Y-m-d H:i:s', $date);
it returns this error strtotime() expects parameter 1 to be string.
if i tried to convert string this DateTime it returns DateTime can not convert to string.
Can you explain what is wrong? Thanks
You already get a native PHP date object from the SQLSRV driver. You don't need to convert anything!
Whenever you need to print it, just use the DateTime::format() method:
echo $row['DateTime']->format('Y-m-d H:i:s');
try with datetime()
$date = new DateTime('2014-06-29 12:00:00'); //$row['DateTime']
echo $date->format( 'Y-m-d H:i:s');
or use directly format your date row
For more read manual :- http://www.php.net/manual/en/class.datetime.php
or may be it's already converted in correct date format so try
echo date('Y-m-d H:i:s', $row['DateTime']);

datetime only returning date

I have a table with column date set to datetime. When i return and get the date from row it is only returning the date and not the time.
$date = $row["date"];
I have tried to format as below and I get the error of:
Warning: date_format() expects parameter 1 to be DateTime, string given
$date = date_format($row["date"], 'Y-m-d H:i:s');
How do I get the whole value?
in your select statement, cast the date into datetime. ex
SELECT CAST(date AS DATETIME) newDate
and retrieve it as
$dateTime = strtotime($row["newDate"]);
try:
$date = date_format(new DateTime($row['date']), "Y-m-d H:i:s");
OR
$date = date('Y-m-d H:i:s', strtotime($row['date']));
You need to convert your string $date to the right type. I had to try a few things to get this right, but this now behaves on my machine:
$thisDate = "2013-02-02 22:17:06"; // example you gave, as a string
$timezone="America/New_York"; // machine was complaining when I didn't specify
$DT = new DateTime($thisDate, new DateTimeZone($timezone)); // this really is a DateTime object
echo $DT->format('Y-m-d H:i'); // you can echo this to the output
$dateString = $DT->format('Y-m-d H:i:s'); // or format it into a string variable
You need to convert the string to date type first. Then date_format() will work. Try the following.
$date = date_format(date_create($row["date"]), 'Y-m-d H:i:s');
Good Luck

Categories