PHP Date Showing '1970-01-01 ' After Conversion

PHP date showing '1970-01-01 ' after conversion

Replace / with -:

$date1 = strtr($_REQUEST['date'], '/', '-');
echo date('Y-m-d', strtotime($date1));

PHP strtotime(): date showing '1970-01-01 ' after conversion

Your format is not a format that the parser understands.

In your case 13 is not a "month". So the parser doesn't understand to date.

You should use DateTime::createFromFormat():

$date = DateTime::createFromFormat('m-d-Y H:i:s','04-13-2018 0:00:53');
echo $date->format('Y-m-d H:i:s');

Output:

2018-04-13 00:00:53

Note that the format could also be: 'm-d-Y G:i:s' with G for "24-hour format of an hour without leading zeros".

Why does this date format continue to change to 1970-01-01?

The strtotime function is sensitive to when you use forward slashes / and dashes -. Below is a quote from php.net to explain.

Dates in the m/d/y or d-m-y formats are disambiguated by looking at
the separator between the various components: if the separator is a
slash (/), then the American m/d/y is assumed; whereas if the
separator is a dash (-) or a dot (.), then the European d-m-y format
is assumed.

Your date is in the european format so the simplest solution would be to replace the forward slashes / with dashes - using the str_replace function.

$date = str_replace("/","-",$_POST["dateOfBirth"]); $dateToTime = strtotime($date); $DOB = date("Y-m-d", $dateToTime);

$licenseNumber = $_POST["licenseNumber"];

$licenseDate = str_replace("/","-",$_POST["licenseExpiryDate"]); $licenseDateToTime = strtotime($licenseDate); $licenseExpiryDate = date('Y-m-d', $licenseDateToTime);

php date format returns 01-01-1970

Please use Now() in query to insert current date. NOW() returns the current date and time.


if(isset($_POST['submit'])){

$post_game = $_POST['game'];

$time = strtotime($_POST['date']);

$post_date = date("d-m-Y", strtotime($time));

if($post_game==''){

echo "<script>alert('Please fill in all fields')</script>";
exit();
}
else {

$insert_game = "insert into last_game (game,date) values ('$post_game',NOW())";

$run_posts = mysqli_query($con,$insert_game);

echo "<script>alert('Post Has been Published!')</script>";

echo "<script>window.open('index.php?last_game_details','_self')
</script>";

}

}

?>

To retrieve your required format of date use php

`echo date_format($date,"Y-m-d ");` 

1970-01-01 stores in the DB when a user tries to input a text in the date field in laravel without showing en error

Your problem is that you convert the user-provided value using strtotime() without checking it first. strtotime('test') will return false, which date() interprets as 0, which is 1970. And then in the custom validation function you are trying to do this conversion again before failing the request for some reason.

Assuming you're using a form request, your code should look something like this:

<?php
namespace App\Http\Requests;

use Carbon\Carbon;
use Carbon\Exceptions\InvalidFormatException;
use Illuminate\Foundation\Http\FormRequest;

class StoreUser extends FormRequest
{
protected function prepareForValidation()
{
// convert from user-input d/m/Y format if possible
try {
$birthday = Carbon::createFromFormat('d/m/Y', $this->date_of_birth);
$this->replace(['date_of_birth' => $birthday->format('Y-m-d')]);
} catch (InvalidFormatException) {
return;
}
}

public function rules()
{
return [
'date_of_birth' => [
'required',
'bail',
'date_format:Y-m-d',
function ($attribute, $value, $fail) {
$age = Carbon::createFromFormat('Y-m-d', $value)->age;
if($age < 18 || $age > 70) {
$fail('Âge invalide. l\'âge devrait être 18-70');
}
}
],
];
}
}

PHP strtotime returns a 1970 date when date column is null

NULL is interpreted as 0 by strtotime, since it want to be passed an integer timestamp. A timestamp of 0 means 1-1-1970.

So you'll have to check for yourself if $row->depositdate === NULL, and if so, don't call strtotime at all.

Avoid displaying 1970-01-01 when strtotime is false

Try this:

 <?php 

if(isset($row['date']) && $row['date'] !="")
{
$date = date("d.m.Y", strtotime($row['date']));
}
else {
$date = "";
}
echo $date ; ?>


Related Topics



Leave a reply



Submit