problem with date() - php

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.

Related

Convert numerical date into Datetime object

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

php convert european datetime to mysql datetime

I've got an datetime string like this: 28-06-14 11:01:00
That's European for day 28, month 6, year 2014...
I'm trying to convert it to 2014-06-28 11:01:00 so that I can insert it into a database with field type datetime.
I've tried multiple things like this:
$datumHolder = new DateTime($data['datum'], new DateTimeZone('Europe/Amsterdam'));
$datum1 = $datumHolder -> format("Y-m-d H:i:s");
$datum2 = date( 'Y-m-d', strtotime(str_replace('-', '/', $data['datum']) ) );
$datum3 = DateTime::createFromFormat( 'Y-m-d-:Hi:s', $data['datum']);
This is the output I get:
datum1: 2028-06-14 11:01:00
datum2: 1970-01-01
And I get an error for datum3:
echo "datum3: " . $datum3->format( 'Y-m-d H:i:s'); . '<br />';
Call to a member function format() on a non-object
What am I doing wrong and how do I get this to work?
Your $datum3 method is correct way, you just have invalid input format.
Use:
$datum3 = DateTime::createFromFormat('d-m-y H:i:s', $data['datum']);
// $data['datum'] is '28-06-14 11:01:00'
$datetime = DateTime::createFromFormat('d-m-y H:i:s', $data['datum']);
// y is two digit representation of a year, while Y is full numeric representation of a year, 4 digits
echo $datetime->format('Y-m-d H:i:s');
$timestamp = strtotime('28-06-14 11:01:00');
$output = gmdate("Y-m-d H:i:s", $timestamp);
echo $output;
That will give you a hint or two ;)

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

Timestamp to Date

Ho do I convert:
2010-12-24 11:39:43
to:
24/12 11:39
Thanks.
This should to the trick:
$newFormat = Date ( 'd/m H:i', StrToTime ( '2010-12-24 11:39:43' ) );
You use StrToTime to convert a string representation of a date to timestamp. You then feed that timestamp to the Date function that takes the format of the date as the first parameter.
Try:
$unixtime = strtotime("2010-12-24 11:39:43");
$newFormat = date("d/m H:i", $unixtime);
echo date("d/m H:i", strtotime("2010-12-24 11:39:43"));
$date = DateTime::createFromFormat('Y-m-d G:i:s', 2010-12-24 11:39:43); //You can simply tell DateTime accept your timestamp as is since PHP 5.3
echo $date->format('d/m G:i T'); //Will output what you wanted + Timezone Abbreviation (because of the T)

php convert date to MySQL date

I need to convert a date in this format:
November 28, 2009
to a MySQL date format:
2009-28-11
What's the best method to convert the date using PHP?
Improvised from: http://www.bigroom.co.uk/blog/dates-in-php-and-mysql
$mysqldate = date( 'Y-m-d', strtotime( $phpdate ) );
// Example:
$phpdate = 'November 20, 2009';
$mysqldate = date( 'Y-m-d', strtotime( $phpdate ) );
echo $mysqldate;
// output: 2009-11-20
If you want a "date" to be converted to "DateTime" this is the best way =
// for example: you have a string with the following
$dateFormat = 'd/m/Y';
$dateString = '02/12/2019';
// you can easily create DateTime using
$dateTime = \DateTime::createFromFormat($dateFormat, $dateString);
As described in the php documentation for createFromFormat.
And to answer completely on your question:
echo $dateTime->format('y-m-d');
I like to use strtotime and the date function as follows:
$mysql_date = date("Y-m-d", strtotime($source_date));
There are two options you can use which are strtotime or preg_split and sprintf. I recommend you use strtotime. The structure goes like this:
$date = 'November 28 2009';
$sqldate = date('Y-m-d', strtotime($date));
Make sure the Y is capital so it reads as 0000 otherwise it will read 00.

Categories